[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


A sed question

Posted on 2006-07-16
Medium Priority
Last Modified: 2010-04-21
Hello Experts,
I want to use sed to find the first occurence of a particular string, for example "foo.bar", in a file.
When the string is found, the entire line it is in is to be replaced by a constant text, for example "surprise".


chicago 999 123
denver 874 99676 X
chicago 654 foo.bar 9867
apache 8794 bar.bar 8994
jersey soprano foo.bar 19

foo.bar is on the 3rd and 5th lines. only the first occurence will be transformed to "surprise".

chicago 999 123
denver 874 99676 X
apache 8794 bar.bar 8994
jersey soprano foo.bar 19

Question by:shalomc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 17119951
This script will do the work, altough not using sed.
create a file named "yourfile" with the lines

chicago 999 123
denver 874 99676 X
chicago 654 foo.bar 9867
apache 8794 bar.bar 8994
jersey soprano foo.bar 19

then run the script below

while read x
a=`echo $x |grep foo.bar`
  if [ $? = 0 -a $flag = 0 ] ; then
         echo surprise
         else echo $x
done < yourfile
LVL 40

Expert Comment

ID: 17126648
Using the ed commands (like vi esc: commands)

ed -s yourfile <<EOX 2>/dev/null

LVL 51

Accepted Solution

ahoffmann earned 2000 total points
ID: 17131299
sed -e '/foo\.bar/s/^.*$/sunprise/' your-file
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 35

Expert Comment

ID: 17135713

 But that would change every line with foo.bar in it (Wouldn't it?) and they only wanted to change the first one.


 sed probably isn't the best tool for this.  It's just not designed to change a single occurrence of a target string like this.

LVL 51

Expert Comment

ID: 17136187
oops, missed the "first one"
this is a academic requirement for sed, you better use awk or perl for that
  awk '/foo\.bar/{if(f==0){print "sunprise";f++;next}}{print}' your-file
LVL 33

Author Comment

ID: 17148151
thanks guys,
this was supposed to work on an AS400 server - IBM did not port awk nor perl.
eventually it turned out that there can be only one occurence of the string, so the points go to ahoffmann


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question