LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-13-2016, 12:24 AM   #1
sam@
Member
 
Registered: Sep 2013
Posts: 31

Rep: Reputation: Disabled
Delete from second part of each line and go to next line


Hi ,
My input file looks like this:

Code:
>AHMI01000729.1 Details of items list product_1, whole details set here
>AHMI01000113.1 Details of items list product_10, whole details set here
>AHMI01000012.1 Details of items list product_100, whole details set here
>AHMI01002223.1 Details of items list product_9953, whole details set here
>AHMI01002226.1 Details of items list product_997, whole details set here
>AHMI01002225.1 Details of items list product_9971, whole details set here
My required output is as follows:


Code:
>item729
>item113
>item12
>item2223
>item2226
>item2225

I tried following sed script:


Code:
sed 's/AHMI01* Details of items list product _*, whole details set here/item/g'  inputfile> outputfile
but it didnt work out!

What could I possibly change?

Thanks!
 
Old 02-13-2016, 01:37 AM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,623
Blog Entries: 40

Rep: Reputation: Disabled
Thumbs down

IGNORE. All wrong. I ignored that you skip the zeros.

Do not laugh at me, but I rather use a programming language than plunge into sed. How about
Code:
for i in `cat in.txt | cut -d' ' -f1|cut -c6-13`; do echo ">item$i";done

... and be it for the record.

Last edited by Michael Uplawski; 02-13-2016 at 01:40 AM. Reason: error in the script example. did not attentively read the OP.
 
Old 02-13-2016, 02:25 AM   #3
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
Quote:
Originally Posted by sam@ View Post
Hi ,
My input file looks like this:

Code:
>AHMI01000729.1 Details of items list product_1, whole details set here
>AHMI01000113.1 Details of items list product_10, whole details set here
>AHMI01000012.1 Details of items list product_100, whole details set here
>AHMI01002223.1 Details of items list product_9953, whole details set here
>AHMI01002226.1 Details of items list product_997, whole details set here
>AHMI01002225.1 Details of items list product_9971, whole details set here
My required output is as follows:


Code:
>item729
>item113
>item12
>item2223
>item2226
>item2225

I tried following sed script:


Code:
sed 's/AHMI01* Details of items list product _*, whole details set here/item/g'  inputfile> outputfile
but it didnt work out!

What could I possibly change?

Thanks!
Hi there. Here you go:

Code:
sed 's/\([^1]*\)\(1[^[1-9]*\)\([^\.]*\).*/>item\3/' file
Code:
sycamorex@darkstar:~/data/tmp$ cat file1
>AHMI01000729.1 Details of items list product_1, whole details set here
>AHMI01000113.1 Details of items list product_10, whole details set here
>AHMI01000012.1 Details of items list product_100, whole details set here
>AHMI01002223.1 Details of items list product_9953, whole details set here
>AHMI01002226.1 Details of items list product_997, whole details set here
>AHMI01002225.1 Details of items list product_9971, whole details set here
sycamorex@darkstar:~/data/tmp$ sed 's/\([^1]*\)\(1[^[1-9]*\)\([^\.]*\).*/>item\3/' file1
>item729
>item113
>item12
>item2223
>item2226
>item2225
 
Old 02-13-2016, 04:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
The sed can reduce the back references a little:
Code:
sed -r 's/[^1]*10*([^.]*).*/>item\1/' file
And awk works too:
Code:
awk -F"[0.]+" '{print ">item"$3}' file
 
Old 02-14-2016, 07:30 PM   #5
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
Quote:
Originally Posted by grail View Post
And awk works too:
Code:
awk -F"[0.]+" '{print ">item"$3}' file
Awk code will fail if there is a zero in the item number!

For example (made up based on previous examples)

Quote:
>AHMI01000709.1 Details of items list product_53243, whole details set here
Would result in
Quote:
item7
which is obviously wrong.


try this, which based on position of the number within the string as it seems constant
Code:
awk '{print ">item" substr($0,8,6)+0}'

Last edited by A.Thyssen; 02-14-2016 at 07:32 PM.
 
  


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
[SOLVED] bash delete part of each line in file nushki Programming 8 04-29-2011 12:42 AM
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 03:36 AM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 03:15 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM

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

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