Solved

A sed question

Posted on 2006-07-16
6
512 Views
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".

Example:

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
surprise
apache 8794 bar.bar 8994
jersey soprano foo.bar 19


ShalomC
0
Comment
Question by:shalomc
6 Comments
 
LVL 6

Expert Comment

by:bira
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

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

Expert Comment

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

---8<---
#!/bin/bash
ed -s yourfile <<EOX 2>/dev/null
/foo.bar/s/^.*$/surprise/
%p
Q
EOX
---8<---


0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 17131299
sed -e '/foo\.bar/s/^.*$/sunprise/' your-file
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Expert Comment

by:James0628
ID: 17135713
ahoffmann,

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


 ShalomC,

 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.

 James
0
 
LVL 51

Expert Comment

by:ahoffmann
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
0
 
LVL 33

Author Comment

by:shalomc
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

ShalomC
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now