By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,396 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,396 IT Pros & Developers. It's quick & easy.

Find string and return next string in bash

P: 1
Hi World,

I am extracting text from an ebcdic header using dd in the cygwin environment (bash/ksh) as below in order to rename the file to something intelligent.

I'm using a specific string position here ($10) but the text I extract is sometimes in a different position in the header so I need to search for preceding text "LINE:" and then extract the next string, which is the info I'm interested in.

So - using awk or sed (or perl if need be) can anyone help me with the syntax for
'find string 'LINE:' and return next string'

Secondly, in order to avoid overwriting files I'm appending a -1, -2, -3 etc but doing a specific search for this.
In awk, how does one read a filename, check to see if there's a -x at the end, and if so, split that off and increment it by one for naming the next file.

Thanks for any tips,
Bryan.

for file in s*.00* ;
do
dd if=$file of=header conv=ascii count=1
linename=`awk '{print $10}' header`

if [ ! -e $linename".sgy" ] ; then
cp $file $linename".sgy"

elif [ -e $linename".sgy" ] && [ ! -e $linename"-1.sgy" ] ; then
cp $file $linename"-1.sgy"

etc until $linename"-4.sgy"

done
Jul 12 '06 #1
Share this Question
Share on Google+
2 Replies

100+
P: 168
> So - using awk or sed (or perl if need be) can anyone help me with the syntax > for 'find string 'LINE:' and return next string'

This is simple with awk. Observe:

[hector@troy ~]$ cat data-file
stuff stuff
LINE:
print me #1
stuff
stuff stuff
LINE:
print me #2
stuff
[hector@troy ~]$ awk '/LINE:/{ getline; print; }' data-file
print me #1
print me #2
Feb 18 '07 #2

100+
P: 168
>Secondly, in order to avoid overwriting files I'm appending a -1, -2, -3 etc but >doing a specific search for this.
>In awk, how does one read a filename, check to see if there's a -x at the end, >and if so, split that off and increment it by one for naming the next file.

I think I follow what you're asking, but maybe you could post a couple real examples of what you're trying to do.
Feb 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.