LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2012, 06:54 PM   #1
medirecpr
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Rep: Reputation: Disabled
AWK - to find a string and save the third sector after


Hello all,

I am trying to write a bash script that can...

find a specific string. I am using:

awk -v RS="~ST" -v FS="*" '{ print $3}'

I run this against a file that has a few "~ST". However, the first line of my output is the first letters of teh file, which do not have an "~ST".

file sample:

ISA*00*<----Sectors---->~ST*837*030702343*<------MOre SEctors------->~CLM*7868*XXX***<-------More SEctors---->~SE*40*030702343~(more "~ST<------>~SE----->" sections)

output sample:

" " ->emtpy
030702343
030702344
030702345

If i print $1 $2 $3:

ISA00
837030702343
837030702344
837030702345

I see that it is grabbing the beggining of the file??? What ????

The second part uses another AWK:

awk -v RS="~CLM" -v FS="*" '{ print $2}' SSS_SELECTO_10092.x12 (which also brings the beggining of the file)

output sample:

00
7868
7869
7870


My plan is to use the output, without the first record. The second part of the process should accomplish:

sed -i 's/030702343/030707868/g' so it updates all transactions or ST<_>SE.

Why does the awk return that first record?
 
Old 04-26-2012, 08:31 PM   #2
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
awk is printing the first record, because you didn't tell it not to.

If you want to skip the first record you need to do something like

Code:
awk -v RS="~ST" -v FS="*" 'NR > 1 { print $3}'
that says effectively "print the third field, but only if the record number (NR) is greater than 1"
 
Old 04-27-2012, 01:06 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
I am not sure why you are confused?

Easy enough to look at your examples:
Code:
awk -v RS="~ST" -v FS="*" '{ print $3}'
This will make the records look like:
Code:
ISA*00*<----Sectors---->
*837*030702343*<------MOre SEctors------->~CLM*7868*XXX***<-------More SEctors---->~SE*40*030702343~(more "
<------>~SE----->" sections)
So the third fields are:
Code:
<----Sectors---->
030702343
#note there is a line here but it is blank as only a single field
Then for example 2:
Code:
awk -v RS="~CLM" -v FS="*" '{ print $2}' SSS_SELECTO_10092.x12
This will make the records look like:
Code:
ISA*00*<----Sectors---->~ST*837*030702343*<------MOre SEctors------->
*7868*XXX***<-------More SEctors---->~SE*40*030702343~(more "~ST<------>~SE----->" sections)
And so second fields are:
Code:
00
7868
You are the one indicating what a record should look like. So where is your confusion?
 
1 members found this post helpful.
Old 04-27-2012, 09:01 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It seems to me that you are forgetting that RS is set to newline by default, and if you change it to something else, then awk ignores the line breaks and only divides the text based on the set string, as grail demonstrated.

Perhaps you just need to re-include the newline in RS to make it do what you want.

Code:
RS="(~ST|\n)"

And please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
 
1 members found this post helpful.
Old 05-01-2012, 04:07 PM   #5
medirecpr
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks David, will use the tags next time!

Quote:
Originally Posted by David the H. View Post
It seems to me that you are forgetting that RS is set to newline by default, and if you change it to something else, then awk ignores the line breaks and only divides the text based on the set string, as grail demonstrated.

Perhaps you just need to re-include the newline in RS to make it do what you want.

Code:
RS="(~ST|\n)"

And please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Thanks david, this helped.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 04:41 AM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 11:16 PM
Save or sort files in sequential order sector by sectyor silencer07 Linux - Newbie 1 08-26-2010 10:31 PM
LXer: PC deal could save public sector billions LXer Syndicated Linux News 0 04-02-2008 06:10 PM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 01:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration