LinuxQuestions.org
Review your favorite Linux distribution.
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 11-19-2011, 12:06 PM   #1
farenheitcx
LQ Newbie
 
Registered: Nov 2011
Posts: 10

Rep: Reputation: Disabled
Question Bash to read text file and replace a line


I need a bash script to find and replace a line with the text "//status_id" to "status_id" and then save the modification into the same file.

I searched in the forum some options with "awk" and "sed" to delete the first two characters but i dont know how exactly to do that.

This is the content of the file to find and replace the text.

Code:
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT

// PERFORMANCE PARAMETERS
cl_graph "32"
mp_inco "3.5"
cl_ficas "22"

// CHECK STATUS
//status_id


cl_graph "32"
mp_inco "3.5"
cl_ficas "22"
cl_graph "32"
mp_inco "3.5"
cl_ficas "22"

Last edited by farenheitcx; 11-19-2011 at 01:27 PM.
 
Old 11-19-2011, 12:14 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Well you could simply replace //status_id with status_id:

Code:
sed 's/\/\/status_id/status_id/' file
or without escaping forward slashes:

Code:
sed 's@//status_id@status_id@' file
 
1 members found this post helpful.
Old 11-19-2011, 12:34 PM   #3
farenheitcx
LQ Newbie
 
Registered: Nov 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Many thanks for your quickly reply. I need some help to perform this script for multiple search and replacing words. I try with this but something is wrong..


Code:
#!/bin/bash
ftxt="//status_id cl_ficas" #find words to replace
rtxt="status_id //cl_ficas" #replace words with this

for u in $ftxt 
do
	for r in $rtxt
	do
		sed 's/\/\/'"{$u}"'/'"{$r}"'/' file
	done
done
 
Old 11-19-2011, 12:46 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
edit: disregard. I didn't read it properly


For variable substitution you need to use double quotes ("). Try and see what happens.

Last edited by sycamorex; 11-19-2011 at 12:48 PM.
 
Old 11-19-2011, 12:51 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
edit: disregard the commands below. It obviously is wrong. the forward slashes are already included in the variables.

I'm not sure if you haven't made it overcomplicated. Try:

Code:
sed "s/\/\/$u/$r/g" file
Once you're happy with the results, add the -i flag to make the changes in the file.

Code:
sed -i "s/\/\/$u/$r/g" file

Last edited by sycamorex; 11-19-2011 at 01:22 PM.
 
Old 11-19-2011, 01:01 PM   #6
farenheitcx
LQ Newbie
 
Registered: Nov 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
I'm not sure if you haven't made it overcomplicated. Try:

Code:
sed "s/\/\/$u/$r/g" file
Once you're happy with the results, add the -i flag to make the changes in the file.

Code:
sed -i "s/\/\/$u/$r/g" file
I will be testing

Last edited by farenheitcx; 11-19-2011 at 01:05 PM.
 
Old 11-19-2011, 01:15 PM   #7
farenheitcx
LQ Newbie
 
Registered: Nov 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Nothing happend. I receive this error

Quote:
sed: -e expression #1, char 19: unknown option to `s'
Code:
#!/bin/bash -x
ftxt="//status_id cl_ficas" #find words to replace
rtxt="status_id //cl_ficas" #replace words with this

for u in $ftxt;
do
        for r in $rtxt;
        do
                sed -i "s/\/\/$u/$r/g" file
        done
done
 
Old 11-19-2011, 01:17 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Sorry, I've been talking to someone and writing on LQ. Hence, rather chaotic and incorrect replies. My apologies. Can't concentrate on 2 things at the same time.


Now, do you need to use loops and variables? Your whole script could be replaced with 1 line:
Code:
sed "s@//status_id@status_id@;s@cl_ficas@//cl_ficas@" file1

Last edited by sycamorex; 11-19-2011 at 01:23 PM.
 
1 members found this post helpful.
Old 11-19-2011, 01:27 PM   #9
farenheitcx
LQ Newbie
 
Registered: Nov 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Smile

Okay, I appreciate your help.
sycamorex good job, thank you for solving my problem, thank you very much!

Solved
 
  


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
Bash: read a text file line by line. stf92 Programming 7 06-11-2012 02:52 PM
bash: replace a line in text file freeindy Programming 10 09-08-2011 12:08 PM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM

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

All times are GMT -5. The time now is 07:22 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