?
Solved

How to use AWK/SED to insert a line?

Posted on 2008-10-01
5
Medium Priority
?
1,565 Views
Last Modified: 2013-12-26
I have a file that looks like this:

Mary=Ann
Ann=Mary
Fake=Fake

Joe=John
Doe=Doe



I want to find Fake=Fake and insert another line before it. This change then needs to be written back to the same file. The replacement value is in a variable, say, $__changes

I have been trying sed but for some reason it creates a 0byte file if the source and target files are the same. I am ok if you can provide an awk solution.

__changes='$1.something'

cat 'file1.txt' | sed '
/Fake=FakeX/ i\
$__changes
' > 'file1.txt'

This doesn't work if I try a variable in the replacement text.

Please note I need to insert before only the first instance of the match.

Thanks for your help.
0
Comment
Question by:infa007
[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
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22613806
>'file1.txt
opens and clears the output before sed runs
try sed -i
0
 
LVL 84

Expert Comment

by:ozo
ID: 22613869
sed -i.bak -e '/Fake=Fake/ i\
'$__changes'
'  file1.txt
0
 

Author Comment

by:infa007
ID: 22615073
I get an error: sed: Not a recognized flag: i
0
 
LVL 1

Accepted Solution

by:
gisellla_igr earned 2000 total points
ID: 22615298
try this

file=file1.txt
tmpFile=${file}$$
__changes="nueva linea"
cat $file | sed "
/Fake=Fake/ i\\
$__changes
" > $tmpFile
mv $tmpFile $file

Open in new window

0
 

Author Closing Comment

by:infa007
ID: 31501945
Thanks, exactly what I wanted!
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

801 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