LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-06-2012, 07:39 AM   #1
bal_nair1
Member
 
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

contains

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

sothat

in the file cde.txt

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

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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


Reply

Tags
copy


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 04:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration