Problem facing with sed and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem facing with sed and awk
# 8  
Old 04-10-2008
Code:
sed -e 's/^[^ ]* *\([1-9][0-9]*\), *\("[^"]*"\),.*/"%\1s", \2/'

The above code seems to work
But I have some doubts here .
Where is this code searching for "column" ( though it worked on the first column statement ) ?
I have many statements beginning with "column". The sed here works for the first found "column". How can i make it work for all the column?

Thanks in advance
JS
# 9  
Old 04-10-2008
Quote:
Originally Posted by era
What does this mean then? You want to print the whole file but replace those lines which are "column" lines?
Exactly !!

1.txt >> parser >> 2.sh

This is "parser" is where am getting stuck with..
# 10  
Old 04-10-2008
Scroll back, I managed to edit my previous reply while you were responding to it.
# 11  
Old 04-10-2008
Hello era,

I tried using the below code as u have given :
cat 1.txt | sed -e 's/^[^ ]* *\([1-9][0-9]*\), *\("[^"]*"\),.*/awk '"'"'BEGIN {printf("%\1s", \2)}'"'"'>> 2.sh/' > temp.txt

This worked for the first occurance of "column"

Where as when i used the code given below, there was no change in 1.txt and 2.sh.

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

I did some changes in the above code and then too it was not making a single difference between the two files.

I think I can go ahead with teh first code. But can u tell me
1. how it is searching the first occurance of " column" ??
2. How can i make code search for all teh firt occurance of the "column " and replace it??

Thanks for teh help ...
JS
# 12  
Old 04-10-2008
It's not searching for the first occurrence. The updated code contains precisely the modification to only match lines which start with "column". Probably there is something in the regular expression which doesn't match your examples exactly. Worked on the samples I copy/pasted from above.
# 13  
Old 04-10-2008
The format of 1.txt is some what like this :

fish in the water
print
column 1, "cat",
column 24, "dog",
column 100, "rat"
hill and jill
run another hat
print column 23, "elephant"
I commit to work
print
column 45, "friend"
column 34, " mother"
The junk stuff I added are other lines of the file 1.txt

Last edited by jisha; 04-10-2008 at 06:22 AM.. Reason: Formatting layout
# 14  
Old 04-10-2008
And you specifically want to turn this into six tiny awk scripts so it won't run too fast?

Sarcasm aside, Franklin's solution looks much closer to what you really ought to be doing.

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

The sed script I wrote before assumes -- based on the examples you gave earlier -- that "column" would always be right at the start of a line. If it's not really, you want to add a " *" after the "^". Lesson: don't obfuscate your examples too much.
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