Sed - using value returned by W+


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - using value returned by W+
# 1  
Old 08-02-2011
Sed - using value returned by W+

Hi,
In a file old.txt containing (for example) a series..
Code:
 
#limerick There was a young lady from Nantucket..
\images\Blank.jpg
:end
#joke A horse walked into a bar...
\images\Blank.jpg
end

I would like to achieve new.txt containing
Code:
 
#limerick There was a young lady from Nantucket..
\images\limerick.jpg
:end
#joke A horse walked into a bar...
\images\joke.jpg
end

I can use
Code:
sed  "/#limerick/,/:end/ s/Blank/limerick/g" old.txt new.txt

and repeat similar for #joke, but as the number of #tags increases, it soon gets out of hand (and slower)

How can I use a command along the lines of
Code:
 sed  "/#\w+/,/:end/ s/Blank/value of w+ without the #/g" old.txt new.txt"

to be able to pass the value of w+ to the replace command?

Obviously going to be a bit more complex than that! Does anyone have any ideas or solutions?

Many thanks
Nigel
# 2  
Old 08-02-2011
Code:
awk -F"[ #]" '/^#/ {print; a=$2; getline; sub(/Blank/,a)}1' infile

# 3  
Old 08-02-2011
Does the below sed helps..?
Code:
sed '/^#/{N;s/^#\([^ ]*\)\(.*\)\\.*$/#\1\2\\\1.jpg/}' inputfile > outfile


Last edited by michaelrozar17; 08-02-2011 at 08:30 AM.. Reason: included ^
# 4  
Old 08-02-2011
Thanks for taking the trouble to reply zaxxon, but I was really hoping to use sed rather than awk.

Michael, many thanks to you too - this works like a charm. Any chance of an explanation or pointing me in the direction of some further reading so I might understand what is happening!

Regards

Nigel
# 5  
Old 08-02-2011
IMO first you would need to learn Regular Expression to govern utilities like sed,awk or perl.
# 6  
Old 08-02-2011
Thanks - I'm reading the sed pages at grymoire at the moment, and having a play with your sed to try and gain a better understanding.

If the text varies, however, it fails:
Code:
 
#limerick There was a young lady from Nantucket..
\images\Blank.jpg
Blank
:end
#joke A horse walked into a bar,
and the barman said
\images\Blank.jpg
:end
other text #joke A bear and a rabbit.
\images\Blank.jpg
:end

to
Code:
 
#limerick There was a young lady from Nantucket..
\images\limerick.jpg
limerick
:end
#joke A horse walked into a bar,
and the barman said
\images\joke.jpg
:end
other text #joke A bear and a rabbit.
\images\joke.jpg
:end

using something like
Code:
/#\w+/,/:end/

to define a start/stop could suit my need better. Is this possible?

Nigel
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Netout: write returned 0?

Hi Am Using unix Aix When i ftp the zero size file getting the netout: write returned 0? (1 Reply)
Discussion started by: Venkatesh1
1 Replies

2. Shell Programming and Scripting

check variable value when nothing is returned

Hi all, I am wondering how can I check when a variable has nothing returned in it. I am trying to store a pid in this variable to see if a script is running in the background. So I am using something like that proc_pid=`ps -ef |grep script.sh|grep -v grep|awk '{print $2}'` if then ... (1 Reply)
Discussion started by: geovas
1 Replies

3. UNIX for Dummies Questions & Answers

What is returned when using 'find . -ls'

Hi all, I tried to find the reply using google, yet did not find a conclusive answer. When I use 'find . -ls', I get something like: 3423297 8 -rw-r--r-- 1 useradmin staff 135 4 apr 09:46 ./.~lock.dir-file-list.csv# I know that 3423297 is the inode -rw-r--r-- ... (2 Replies)
Discussion started by: dakke
2 Replies

4. UNIX for Dummies Questions & Answers

Delete files returned by ls

I need to delete the files that are returned by this ls command: root@16:06:37@whci03:/var/log/whci# ls -ltrhS | grep "Apr " -rw-r--r-- 1 whci users 180M Apr 4 00:00 20090403_whci_Access.txt -rw-r--r-- 1 whci users 185M Apr 4 00:00 20090403_whci_Access.txt -rw-r--r-- 1 whci users 189M... (1 Reply)
Discussion started by: whci98
1 Replies

5. HP-UX

about the value returned from oracle

Hi all can i use unix variable holds the data returned by oracle in a shell script? x=sqlplus -s u/p@h <<! >>fg.log whenever sqlerr exit 1 select count(*) from schema.mat_view; exit ! echo $x Will the $x return the value of count(*)? thanks megh (1 Reply)
Discussion started by: megh
1 Replies

6. Shell Programming and Scripting

How to assign value to a variable with row(using -n) returned by sed

Hi Friends, REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt set -x i=1 echo "i=$i" COUNT=`sed -n '$=' test.txt` echo "Count=$COUNT" while do "## Here is error##" FILETOREMOVE=`sed -n \'$i,1p\' test.txt` echo $FILETOREMOVE... (5 Replies)
Discussion started by: sourabhsharma
5 Replies

7. HP-UX

returned from remote command

Hi, there, I want to excute the remote command shell via "remsh", are there any simple or best way to get the result of remote shell from local ? thanks. (2 Replies)
Discussion started by: Frank2004
2 Replies

8. HP-UX

ridst returned

The rdist command script is called in the program to synchronize the updated files, I wonder whether the rdist returned can be fetched or not from the program? (0 Replies)
Discussion started by: Frank2004
0 Replies

9. Shell Programming and Scripting

-1 returned from fetchall_arrayraf

Hi all, below is my PERL script $ids = $dbh->prepare ("select * from tableA"); $ids->execute(); $names = $ids->fetchall_arrayref(); print " Total valid ids is $#names \n"; Table only has 1 column, approx 400 rows the value of $#names is -1. Would anyone point out what could have caused the... (1 Reply)
Discussion started by: new2ss
1 Replies

10. UNIX for Advanced & Expert Users

w/who command returned zero users

Hi ppl, We are using Sun OS and recently, encountered this strange problem. When issuing the "w" or "who" command, the system produce no listing. See screen shot below. ----------------------- $ w 2:56pm up 2 day(s), 21:10, 0 users, load average: 1.03, 0.75, 0.69 User tty ... (6 Replies)
Discussion started by: sinyem
6 Replies
Login or Register to Ask a Question