LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-07-2010, 12:25 AM   #1
geodave0110
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu
Posts: 6

Rep: Reputation: 0
Finding and deleting a sequence of letters


Hello all!

I'm fairly new to scripting in Linux, and I'm trying to write a script to do the following:

I have a text file that contains many lines that look like this:

>title_1
OENNDTESFGGHPQIENSAFGTYYYNF

>title_2
TWEPTNGHHPSPAWPQIENFFFKLHGS

I'm trying to make my script read this text file, find the string sequence "QIEN", and delete everything from this sequence backwards (including "QIEN") so that the above lines look something like this:

>title_1
SAFGTYYYNF

>title_2
FFFKLHGS

I'm aware that grep is good to do a regular find-and-delete as follows:
Code:
grep -Ev 'QIEN'
But this will delete everything on the lines that contain the string sequence "QIEN".

Any ideas or suggestions will be much appreciated!

geodave

Last edited by geodave0110; 12-07-2010 at 12:27 AM.
 
Old 12-07-2010, 12:43 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
When you need to modify lines, you'll generally use sed instead.
Code:
sed 's/^.*QIEN//' file.txt
Note that this doesn't modify the original file. Use sed's -i option to do that, once you're satisfied with the results.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Another possibility is to use bash's built-in parameter substitution, and loop through each line of the file.
Code:
while read line; do
	echo "${line#*QIEN}"
done <file.txt  >newfile.txt
Bash loops can't modify the original either. You have to redirect the output into a new file, as shown in the last line.

Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
 
Old 12-07-2010, 12:54 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Code:
t=asdqienzxc
echo ${t#*qien*}
zxc
http://tldp.org/LDP/abs/html/string-manipulation.html
http://tldp.org/LDP/abs/html/refcards.html#AEN22102
 
Old 12-08-2010, 02:14 PM   #4
geodave0110
LQ Newbie
 
Registered: Dec 2010
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you guys for your prompt replies.

David the H.'s sed command worked like a charm. Those links you provided are going to be very useful to me as I teach myself Linux scripting. Thank you!

geodave
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk - finding and counting words specific letters within mora978 Programming 9 10-13-2010 11:45 AM
Help finding gaps in a sequence mikeleigh Programming 10 08-20-2009 06:13 PM
getting greek letters? buffed317 Linux - General 2 09-11-2004 03:43 PM
space between letters nicoc Mandriva 5 09-03-2004 01:32 PM
saving my letters blotch Linux - Newbie 4 11-28-2001 05:47 PM


All times are GMT -5. The time now is 01:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration