awk Help - Beginner | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

awk Help - Beginner

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
mcclunyboy mcclunyboy is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 29 November 2013, 10:38 AM EST
Posts: 90
Thanks: 16
Thanked 1 Time in 1 Post
awk Help - Beginner

Hi,

I think I need to use AWK - however I have no experience of it. Can someone help please?

I have a file like this but with many more records - it is fixed width

Code:
THIS15021X            799999 XX           00000099999 00008888888 XX 15022013
THISQ15021X      999999 XX           00000099999 00008888888 XX 15022013
BLAS15021X          9999999XX        00000199999 00008888888 XX 15022013

I want the file to look like this again it is fixed width

Code:
THIS15021X         0799999 XX          00000099999 00008888888 XX 15022013
THISQ15021X     0999999 XX          00000099999 00008888888 XX 15022013
BLAS15021X        9999999 XX          00000199999 00008888888 XX 15022013

I want to add a leading 0 to the 2nd coloumn if it contains the word this at the beginning

Last edited by zaxxon; 02-20-2013 at 07:38 AM..
Sponsored Links
    #2  
Old 02-20-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk '/^THIS/{$2="0"$2}1' file

Sponsored Links
    #3  
Old 02-20-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 31 October 2014, 11:19 AM EDT
Location: Simplicity
Posts: 1,993
Thanks: 129
Thanked 584 Times in 529 Posts
In shell with built-in commands:

Code:
while read word1 word2 otherwords
do
  case $word1 in
  THIS*) word2="0$word2";;
  esac
  printf "%s %s %s\n" "$word1" "$word2" "$otherwords"
done < file > newfile

In shell by using awk:

Code:
awk '/^THIS/ {$2="0"$2} {print}' < file > newfile

Both solutions read from file and direct their output to newfile .

Last edited by MadeInGermany; 02-20-2013 at 07:23 AM.. Reason: fix
    #4  
Old 02-20-2013
mcclunyboy mcclunyboy is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 29 November 2013, 10:38 AM EST
Posts: 90
Thanks: 16
Thanked 1 Time in 1 Post
Thanks, that is very close - however I have noticed a funny problem:

So this is the actual file:


Code:
THIS15021X  123455 X            00000038600 00009999999 XX 15022013
THIS15021X   123456 XX           00000004260 00009999999 XX 15022013
THIS15021X   123457 XX            00000002240 00009999999 XX 15022013
THIS15021X   123458 XX            00000000800 00009999999 XX 15022013
THIS15021X   123459 XX            00000023445 00009999999 XX 15022013
THIS15021X   123460 XX            00000006632 00009999999 XX 15022013
BLAS15021X  1123456 XX            00000178839 00009999999 XX 15022013

However after running the commans, specificall the first one (note I am using a space before the 0 to move it along to the right position - the problem occurs without this space as well):


Code:
 awk '/CHEQ/ {$2=" 0"$2;}1' < file.txt > tempfile.txt

I am receiving this, the "BLAS" record is now incorrect:


Code:
THIS15021X   0123455 XX            00000038600 00009999999 XX 15022013
THIS15021X   0123456 XX            00000004260 00009999999 XX 15022013
THIS15021X   0123457 XX            00000002240 00009999999 XX 15022013
THIS15021X   0123458 XX            00000000800 00009999999 XX 15022013
THIS15021X   0123459 XX            00000023445 00009999999 XX 15022013
THIS15021X   0123460 XX            00000006632 00009999999 XX 15022013
BLAS15021X   1123456 XX                               00000178839 00009999999 XX 15022013

Will I need to specify the order in it's entirety $1 $2 $3 or something?
Sponsored Links
    #5  
Old 02-20-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 31 October 2014, 12:14 PM EDT
Location: Aachen, Germany
Posts: 4,485
Thanks: 74
Thanked 1,099 Times in 1,035 Posts
Quote:
Originally Posted by mcclunyboy View Post
. . .
I want the file to look like this again it is fixed width
. . .
I think all the proposals up to now will lose the fixed width property which the requestor needed to keep, esp. if you just add a "0" in front of $2 the record size is gone.
Not sure, though, how to do it. Break it into lines with fmt , modify with awk or sed , remove <linefeed> with tr ?
Were there single lines, this might work:
Code:
$ sed 's:\(^THIS[^ ]*  \) :\10:' file


Last edited by RudiC; 02-20-2013 at 08:33 AM..
Sponsored Links
    #6  
Old 02-20-2013
mcclunyboy mcclunyboy is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 29 November 2013, 10:38 AM EST
Posts: 90
Thanks: 16
Thanked 1 Time in 1 Post
Can you talk me through that sed command - with a slight edit I "think" I get what I want:


Code:
> sed 's:\(^THIS[^ ]* \) :\00:' file

Specifically the 10, which I replaced to 00... - simply adds a space followed by a 0 to the location I require.

Last edited by mcclunyboy; 02-20-2013 at 09:36 AM..
Sponsored Links
    #7  
Old 02-20-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 31 October 2014, 11:19 AM EDT
Location: Simplicity
Posts: 1,993
Thanks: 129
Thanked 584 Times in 529 Posts
This is more close to your initial requirement:
if there is THIS at the beginning of the line, insert a 0 at the beginning of the 2nd column.
The only additional requirement: columns are separated by one or many space characters.

Code:
sed '/^THIS/ s/ \([^ ]\)/ 0\1/' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK for a beginner omega3 Shell Programming and Scripting 4 09-20-2012 05:59 AM
""Help Me!""Beginner awk learning issue csrohit Shell Programming and Scripting 6 03-05-2012 03:55 AM
Beginner Help thibodeau Shell Programming and Scripting 5 01-30-2010 05:08 PM
AWK help please - beginner COLLEGE UNIX for Dummies Questions & Answers 4 06-13-2007 07:40 AM
Please help. I am a beginner. Lykathea Aflame Shell Programming and Scripting 1 04-26-2006 01:46 AM



All times are GMT -4. The time now is 02:01 AM.