Visit the LQ Articles and Editorials section
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


Search this Thread
Old 06-06-2012, 07:39 AM   #1
Registered: Apr 2012
Location: Bangalore,India
Posts: 41

Rep: Reputation: Disabled
Smile how to copy the words after a particular word in a line

Hi all,

I have a requirement in my bash script, please help me out

suppose I have a file abc.txt


sdsdx dslkdskldj sdjksjd sdczlz
sssdsk sdsdx udsuids dsjiosd jsids
sdsj ncmcxu sdsdx ionmqwas pjsqax

I would like to copy the words after and including sdsdx from all the lines in the file abc.txt and copy to a file cde.txt


in the file cde.txt

sdsdx dslkdskldj sdjksjd sdczlz
sdsdx udsuids dsjiosd jsids
sdsdx ionmqwas pjsqax
Old 06-06-2012, 07:59 AM   #2
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,558

Rep: Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466Reputation: 1466
You can do it with sed, cut or awk, but just with bash also.
make while loop, use readline to read the input file line by line and use parameter substitution to cut text in front of sdsdx. Finally print the result into the target file
Old 06-06-2012, 08:17 AM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 730Reputation: 730Reputation: 730Reputation: 730Reputation: 730Reputation: 730Reputation: 730
sed 's/.*\(sdsdx.*$\)/\1/' abc.txt > cde.txt



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 09:41 PM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 07:35 AM
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 04:42 AM
copy files containing specific words in a specified line abenmao Linux - Newbie 5 08-28-2008 10:04 AM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 02:57 PM

All times are GMT -5. The time now is 02:46 AM.

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