Solved

How to use AWK/SED to insert a line?

Posted on 2008-10-01
5
1,544 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
  • 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 500 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

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 82
fizzArray  challenge 1 47
wordappend challenge 8 85
Problem to event 3 50
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

19 Experts available now in Live!

Get 1:1 Help Now