LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2013, 05:29 AM   #1
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
how to replace a line with unknown spaces in a file using shell script


Hi,

[ravi@ravi ~]$ cat text_file
biscuit 1

I want to replace the string "biscuit 1" with "biscuit 0" using a command or with a script.

I tried the following perl command:
Quote:
perl -pi -w -e 's/biscuit\ \ \ \ \ \ 1/biscuit\ \ \ \ \ \ 0/g' text_file
The limitation here is.. it works only if the space between the "biscuit" word and the "number(0 or 1)" is exactly 6(as i use the 6 escape characters).

I want it to work..with unknown number of spaces between the word and the number.

Actually i need it vice versa.. i.e if the file is having "biscuit 0" the command/script should replace it to "biscuit 1" & if the file is having "biscuit 1"..the command/script should replace it to "biscuit 0".

Please help!!

Thanks,
Ravi
 
Old 01-29-2013, 06:13 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
\s+ represents any non-zero amount of whitespace.
 
1 members found this post helpful.
Old 01-29-2013, 06:27 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Can you try awk:-
Code:
~$ gawk 'BEGIN{FS=" "}; {if($2 == 0) gsub(/0/,"1",$2); else if ($2 == 1) gsub(/1/,"0",$2); print $0}' infile.txt

Last edited by shivaa; 01-29-2013 at 12:16 PM. Reason: Added a missing curly braces i.e. } at the end of code. Corrected.
 
1 members found this post helpful.
Old 01-29-2013, 07:03 AM   #4
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
Somehow, Got it
[ravi@ravi ~]$ cat text_file
biscuit 1
[ravi@ravi ~]$ cat code
#!/bin/sh
count=$(grep -i biscuit text_file | awk '{print $2}')
if [ "$count" == 1 ]; then
perl -pi -w -e 's/biscuit\s+\ 1/biscuit\t\t\t\t 0/g' text_file;
else
perl -pi -w -e 's/biscuit\s+\ 0/biscuit\t\t\t\t 1/g' text_file;
fi
[ravi@ravi ~]$ cat text_file
biscuit 1
[ravi@ravi ~]$ ./code
[ravi@ravi ~]$ cat text_file
biscuit 0
[ravi@ravi ~]$ ./code
[ravi@ravi ~]$ cat text_file
biscuit 1

---------- Post added 01-29-13 at 06:34 PM ----------

Thanks linosaurusroot, "\s+" helped me!!

Last edited by RaviTezu; 01-29-2013 at 07:04 AM.
 
Old 01-29-2013, 10:02 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
perl -pi -w -e 's/(biscuit\s+) 1/\1 0/g' text_file
 
  


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
Shell Script - Cant get sed to replace a line from a textfile CrowdstarQA Programming 4 02-23-2012 11:53 AM
shell script replace a line from a file dthanoon Programming 11 03-05-2010 09:46 AM
How to replace line with another line in shell script pramod.srk Linux - Newbie 5 02-26-2010 06:00 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:49 PM.

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