Solved

A sed question

Posted on 2006-07-16
6
518 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
[X]
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
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 35

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

688 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