awk to find a formated o/p


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to find a formated o/p
# 1  
Old 10-17-2008
awk to find a formated o/p

hi i have a file which is having

a.txt

name aaa
work business
emp since 2oct
office delhi

name aba
work business
emp since 6oct

name abc
work shopper
emp since 4oct
office kolkata


op
name : aaa emp since :2oct
name : aba emp since :6oct
name : abc emp since :4oct


i want o/p like above is it possible using awk or sed
pls help me
# 2  
Old 10-17-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk -F'\n' '$0="name: "$1OFS$3' RS= infile

Just noticed the last colon:

Code:
awk -F'\n' '{sub(/since /,":");print"name: "$1OFS$3}' RS= infile

# 3  
Old 10-17-2008
hi thanks 4 reply
but when i am running this it is giving op
name: name aaa emp : 2oct
name: work shopper

which is not as i desired (2nd line)
pls look it
# 4  
Old 10-17-2008
Yep,
the code was wrong.
Is the below output correct?

Code:
$ cat file
name aaa
work business
emp since 2oct
office delhi

name aba
work business
emp since 6oct

name abc
work shopper
emp since 4oct
office kolkata

$ nawk -F'\n' '{sub(/ /," : ",$1);sub(/since/,"& :",$3);print $1,$3}' RS= file
name : aaa emp since : 2oct
name : aba emp since : 6oct
name : abc emp since : 4oct

# 5  
Old 10-17-2008
thaks a lot
its working fine
can u pls explain the code
# 6  
Old 10-17-2008
Yes.
Setting the RS to empty string means that records are separated by one or more blank lines. So after setting the FS to a new line we have:

Code:
$1 = name aaa
$2 = work business 
...

Then we just make the required substitutions (the colon after name and since) and print the desired fields.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace in awk

I have a file that I am trying to find a specific word, then replace text within that string. file TestA2015 TestB2016 Example. Replace TestB2016 to TestB0000, so if TestB is found replace the original "2016" to "0000". Thank you :). awk tried awk '{ sub(/TestB$/, "0000", $6) }1'... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

3. Filesystems, Disks and Memory

Recover a mistaken swap-formated USB-HD

Hi! Installing the debian wheezy by netinstall my external USB-HD (2TB) is erroneously completly formated as swap-filesystem. I was to lazy to disconnect the USB-HD, so now i could kick myself . Is there any chance to rescue the data. I tried to find a way by using gparted: the whole HD is... (3 Replies)
Discussion started by: IMPe
3 Replies

4. Shell Programming and Scripting

Using awk to find sentences.

I am trying to print out sentences that meets a regular expression in awk (I’m open to using other tools, too). I got the regular expression I want to use, "(\+ \{4\})" from user ripat in a grep forum. Unfortunately with grep I couldn't print only the sentence. While searching for awk... (8 Replies)
Discussion started by: danbroz
8 Replies

5. Shell Programming and Scripting

Awk find and sel

Hi to all! I need to make an script to find when an user changes the IP. The log file have this simple structure; example.txt Jack 192.168.1.2 Tom 192.168.12.225 Mary 192.168.1.22 Jack 192.168.1.5 Patrick 192.168.1.88If match the same user in the first column and have differents IP in... (3 Replies)
Discussion started by: LordXeno
3 Replies

6. Shell Programming and Scripting

Find avg using awk

Hi, i need some help plz... The file data.txt contains: code of student,surname and name,code of lesson,grade of lesson.The number of lessons of each student is not the same. 25,Jackson Steve,12,4,34,2,65,2 29,Jordan Mary,13,6,23,8,56,4,34,2 04,Leven Kate,14,6,15,6,26,4 34,Owen... (10 Replies)
Discussion started by: Steve_09
10 Replies

7. Shell Programming and Scripting

Create a formated text file

Hi, I have written a BASH shell script (included below) which will allow me to monitor my blood pressure. The script computes the mean of 5 user input systolic, diastolic, and heart rate values. I would like the script to then append these three values to their respective columns in a text... (5 Replies)
Discussion started by: msb65
5 Replies

8. Shell Programming and Scripting

processing tab-formated output of command w/bash

I have a command that when ran it will have an output such as string LongerString string2 longerString2 More MoreStrings seperated by tabs. The command lists domains and their accounts set up in my server admin software (interworx). The end result will be that it will run rsync for... (2 Replies)
Discussion started by: sweede
2 Replies

9. Windows & DOS: Issues & Discussions

1.44mb disk formated = 1.38mb left??

I was formating a couple of floppy disks, to make room for a ftp install of suse. However, the files you are suposed to put on the floppys are 1.4 mb. I thought "Fine, I have a 1.44 mb disk here". But appearantly windows uses 600kb on....tmp files or something. If anyone knows how I might fix... (4 Replies)
Discussion started by: G-wizz
4 Replies

10. SuSE

Disk Formated with NTFS

HI All, My pc has two disks, One disk loaded with windows NT 2000 and redhat linux 7.2 dual boot. Another disk 80GB added and formated with NTFS file system. How to access the 80GB NTFS file system disk by booting linux os. Thanks in advance Bache Gowda (4 Replies)
Discussion started by: bache_gowda
4 Replies
Login or Register to Ask a Question