Solved

A sed question

Posted on 2006-07-16
6
506 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
Comment Utility
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 39

Expert Comment

by:noci
Comment Utility
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
Comment Utility
sed -e '/foo\.bar/s/^.*$/sunprise/' your-file
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Expert Comment

by:James0628
Comment Utility
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
Comment Utility
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 32

Author Comment

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

762 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

7 Experts available now in Live!

Get 1:1 Help Now