Problem facing with sed and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem facing with sed and awk
# 1  
Old 04-09-2008
CPU & Memory Problem facing with sed and awk

Hi All,

I have a got a problem ..
I have t files as below:

1.txt contains
Code:
-----
-----
-----
column  1,  "cat",
column  24,  "dog",
column  100,  "rat",
-----
-----
-----

2.sh should contain
Code:
-----
-----
-----
awk 'BEGIN { printf ("%1s","cat")}' 
awk 'BEGIN { printf ("%24s","dog")}'
-----
-----
-----

When i execute 2.sh i should get the formatted printed output

I have tried the following codes;

awk '/column/ { print <> }' format.txt > we
where the <> is awk 'BEGIN .....
The problem with this command is I cannot get the lines above or below the search.
i.e only the lines containng "column" will be replaced and written to 2.sh

If i use sed, I cannot get the fileds from the file 1.txt
i.e; In order to get the "%1s" "%100s" from file 1.txt is not possible.

Is their any way we can overcome these two problems ?

thanks in advance
JS

Last edited by Yogesh Sawant; 04-09-2008 at 09:44 AM.. Reason: added code tags
# 2  
Old 04-09-2008
If I understand the question you can do something like:

Code:
awk '{gsub(",|\"","");printf("%*s\n", $2, $3)}' 1.txt

Regards

Last edited by Franklin52; 04-09-2008 at 05:02 PM.. Reason: combining gsub command
# 3  
Old 04-10-2008
Hi Franklin52,

Thank you for the reply.
The code does't give me the output i need. But i think a bit a tuning can make it do.

Can u explain me how the code works here.

Thanks a lot
JS
# 4  
Old 04-10-2008
The gsub function removes the comma's and the quotes.
The printf function uses the value of $2 for the width option to format the output.

Regards
# 5  
Old 04-10-2008
Understanding the context of this problem would probably help. Your first awk script specifically looks only for lines containing "column"; if that's not what you want, why do you do that?

Getting a number which is always the second field on a line with sed is not at all impossible.

Code:
sed -e 's/^[^ ]* *\([1-9][0-9]*\), *\("[^"]*"\),.*/"%\1s", \2/'

Franklin's code simply removes the commas and the double quotes (from between the fields, and around the third field, but for simplicity anywhere else too), then uses the selected fields in a printf directly.
# 6  
Old 04-10-2008
Am specifically looking for lines that are beginning with "column" and replacing those lines with an " awk" command

In Short if i have a line :
column 1, "cat",
I need to replace this line with :
awk 'BEGIN { printf ("%1s","cat")}' ( given that no change should occur to the other lines in the file including the line numbers )

I need a parser to convert these lines.

Thanks to all
JS
# 7  
Old 04-10-2008
Quote:
Originally Posted by jisha
The problem with this command is I cannot get the lines above or below the search.
i.e only the lines containng "column" will be replaced and written to 2.sh
What does this mean then? You want to print the whole file but replace those lines which are "column" lines?

Code:
sed -e 's/^column *\([1-9][0-9]*\), *\("[^"]*"\),.*/awk '"'"'BEGIN { printf ("%\1s", \2) }'"'/"

Still a bigger context would be useful for understanding the problem and your attempt at a solution, at it seems highly inefficient to create umpteen simple awk scripts with only a BEGIN part.

Last edited by era; 04-10-2008 at 04:16 AM.. Reason: Missing close quote
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting with awk: facing problem

Hi, I wants to print the 9th column information with its path name in some txt file. Here is one line which works fine for me: rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > dataFilenames.list rfdir=="ls -ltr" ... (2 Replies)
Discussion started by: nrjrasaxena
2 Replies

2. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

3. Shell Programming and Scripting

Problem facing in using awk command

Hi., I am not able to replace the string with another string using gsub fn of awk command. My code: awk 'BEGIN gsub(004,IND,004)' p.txt and my i/p file p.txt is of the format: av|004|adkf|Kent,004|s av|005|ssdf|Kd,IT park|s . . . and my desired o/p should be of : (13 Replies)
Discussion started by: av_vinay
13 Replies

4. Solaris

Facing Problem with metaset in SVM

hi all, i am using solaris 5.10 on sun blade 150 and i am trying to configure diskset in sun volume manager. When i fire the following command, it says some rpc related error. bash-3.00# metaset -s kingston -a -h u15_9 metaset: u15_9: metad client create: RPC: Program not registered how to... (4 Replies)
Discussion started by: kingston
4 Replies

5. Shell Programming and Scripting

problem facing in if -else condition

can u plz tell me where is the error echo enter the filename to be searched read fname if #-d $fname then echo file exists if then echo itsa directory elif then echo its readable cat $fname else echo its not readable fi else ... (1 Reply)
Discussion started by: gotam
1 Replies

6. Infrastructure Monitoring

Facing problem while configuring snmp

HI, I am facing these two errors while configuring snmp can any body guide me. vi /var/log/snmpd.log Error opening specified endpoint "udp:161" Server Exiting with code 1 i also tried bash-3.00# netstat -a | grep snm *.snmpd Idle bash-3.00# lsof -i :161 bash: lsof: command not... (2 Replies)
Discussion started by: nir1785
2 Replies

7. Solaris

Facing problem with zone

i am using this way to create zone1 and zone2 bash-2.05b# zonecfg -z zone1 zone1: No such zone configured Use 'create' to begin configuring a new zone. zonecfg:zone1> create zonecfg:zone1> set zonepath=/zone/1 zonecfg:zone1> set autoboot=true zonecfg:zone1> add net zonecfg:zone1:net>... (6 Replies)
Discussion started by: coxmanchester
6 Replies

8. Solaris

please help as i am facing problem with uptime

Hi I am getting the uptime output as follows 12:40am up 4 day(s), 18:29, 2 users, load average: 38.97, 36.54, 34.89 The load average is too high . I have checked the processes , but no process is taking too much cpu time Please help (3 Replies)
Discussion started by: guy009
3 Replies

9. UNIX for Dummies Questions & Answers

facing a problem in redirection

Hi, I am doing this perl script print (@line(1..15)); the lines 1 to 15 get printed... how can i redirect this to file? thanks and regards vivek.s (4 Replies)
Discussion started by: vivekshankar
4 Replies
Login or Register to Ask a Question