LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2012, 04:58 PM   #1
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Rep: Reputation: 15
Script to insert line into file that contains multiple """ characters


Hi

I am attempting to insert a line into a file using a script. I have seen lots of examples online using sed or awk but i cannot get either to work as the line I am inserting has multiple " in it.
The line i need to insert is

Code:
Option "InvertX" "1"
I tired with:
Code:
sed -e '4a\Option "InvertX" "1"' temp.txt
awk 'NR==4{print "Option "InvertX" "1""}1' temp.txt > temp2.txt
but neither of these work because of the multiple """".

Is there an escape character for the "'s that I don't know about??

Thank you.
 
Old 02-15-2012, 05:23 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Code:
sed -e '4a\Option "InvertX" "1"' temp.txt
The above command works on my system.

Code:
$ cat temp.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
$ sed -e '4a\Option "InvertX" "1"' temp.txt
Line 1
Line 2
Line 3
Line 4
Option "InvertX" "1"
Line 5
Line 6
So, that begs the question: are you showing the real, actual command you are using? Or did you modify it when you posted the questions?

Similarly, are you using the command exactly as you describe?

EDIT:
1. On the off-chance that it's a misunderstanding on how sed works: sed does not modify the file by default. Sed prints the result of the modification(s) to the screen unless you explicitly tell it to do otherwise. You can tell it to do otherwise using output redirection (just like you did with your awk command) or use the '-i' option with sed (which tells sed to modify the original file). If you use the '-i' option, then read the man page for sed (man sed) regarding that option and how you can make an automatic backup of the file (in case your sed instructions don't give you what you want).

2. Regarding your awk command... Fix it by escaping the internal double quotes:
Code:
awk 'NR==4{print "Option \"InvertX\" \"1\""}1' temp.txt > temp2.txt
NOTE: The results of the two commands you gave are different. Your sed command adds the new text after the 4th line (as shown in my sample output)--because you are "appending" the text on the fourth line (i.e. the '4a' in the command).

Compare that with the awk command that adds the new text after the 3rd line.

If the exact line matters, then make sure you use the appropriate command (or modify it accordingly).

Last edited by Dark_Helmet; 02-15-2012 at 05:41 PM.
 
Old 02-15-2012, 06:43 PM   #3
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
Yes they are the real commands i had tried. It is to invert the x axis in the config for a touchscreen. I only do scripting very occassionally so it get forgotten.

I used awk using the \ escape character.

Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] "Insert" & "Delete" key returns "~" in a terminal. sharky Linux - General 15 04-26-2011 08:36 AM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Can I redirect script output to a file without ">> $LOGFILE" at the end of each line davee Linux - General 1 12-19-2003 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration