|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
awk '/^THIS/{$2="0"$2}1' file |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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 > newfileBoth solutions read from file and direct their output to newfile . Last edited by MadeInGermany; 02-20-2013 at 07:23 AM.. Reason: fix |
|
#4
|
|||
|
|||
|
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.txtI 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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|