LinuxQuestions.org
Review your favorite Linux distribution.
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 08-12-2012, 04:24 AM   #1
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Rep: Reputation: Disabled
start the line only with numbers


hi,

I need some unix command to replace the following thing. The line shuld start with oly numbers. If it starts with anything other than number it shuld be taken back to the last line.

My file:

1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break

Output should be like below:
1234|testweye|test1|break
576|test|break|title
2369|test|line|breaktite|break
234589|test|like|break
 
Old 08-12-2012, 04:38 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
My apologies. I misread the output file, and asked an erroneous question.

Last edited by jschiwal; 08-13-2012 at 07:42 AM.
 
Old 08-12-2012, 05:01 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
This is the closest I come without any more explanation:
Code:
tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/'
This does add an extra | when lines are appended.

Example:
Code:
$ cat infile
1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break

$ tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/'
1234|test|weye|test1|break
576|test|break|title
2369|test|line|break|tite|break
234589|test|like|break
Not too elegant, but it seems to be doing the job.
 
Old 08-12-2012, 05:08 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 702

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
Hi.
Here is another sed solution:
Code:
$ cat infile
1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break
$ sed -n '/^[0-9]/{:a; N; /\n[0-9]/be;  s/\n//; ba}; :e;p' infile
1234|testweye|test1|break
576|test|break|title
2369|test|line|breaktite|break
234589|test|like|break
The idea is almost the same as here.

Last edited by firstfire; 08-12-2012 at 05:12 AM.
 
Old 08-12-2012, 06:51 AM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
nevermind

Hi,
A grep solution
Code:
bash-4.2$ tr '\n' '|' < infile |grep -Eo  "[[:digit:]]*[[:alpha:]|]*"
1234|test|weye|test
1|break|
576|test|break|title|
2369|test|line|break|tite|break|
234589|test|like|break|

Last edited by whizje; 08-12-2012 at 07:05 AM.
 
Old 08-12-2012, 07:59 AM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Repaired grep
Code:
bash-4.2$ tr '\n' '|' < infile |grep -Eo  "[[:digit:]]*([|]+[[:digit:]]*[[:alpha:]]+[[:digit:]]*)*"
1234|test|weye|test1|break
576|test|break|title
2369|test|line|break|tite|break
234589|test|like|break

Last edited by whizje; 08-12-2012 at 08:06 AM.
 
Old 08-13-2012, 12:13 AM   #7
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
HI,

When i use
1. sed -n '/^[0-9]/{:a; N; /\n[0-9]/be; s/\n//; ba}; :e;p' infile
i am getting Label to long error.

2. tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/' > outfile
0 byte file is received.

Please help me.

Last edited by anshaa; 08-13-2012 at 12:25 AM.
 
Old 08-13-2012, 12:25 AM   #8
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
sed -n '/^[0-9]/{:a; N; /\n[0-9]/be; s/\n//; ba}; :e;p' infile
i am getting Label to long error.
 
Old 08-13-2012, 03:34 AM   #9
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Cool

nawk 'END{print RS} /^[0-9]/{if(NR>1)print RS}1' ORS= filename > filename1

Works perfectly.
 
Old 08-13-2012, 04:11 AM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 702

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
Hi.

You probably have an ancient version of sed.
 
  


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] Trying to number every other line and append those numbers to end of line kmkocot Programming 7 04-23-2010 12:17 PM
show line numbers in vi swamprat Linux - Newbie 1 12-28-2008 05:55 PM
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 05:22 AM
Prepend # for certain line numbers in VI twantrd Linux - Software 3 09-05-2005 02:27 PM
line numbers in kdevelop febisfebi Programming 1 02-18-2004 08:45 PM


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