Add string number to lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add string number to lines
# 1  
Old 04-11-2013
Add string number to lines

So I have a file in the form

Code:
>
akdfvcnciejcndmdjfk
>
kdjkkkifjeeeeelfjfuf
>
fjfhchdejhfhfhfhfhfhf
>
skdkdhfhvnvncnccm

and I would like it to come out in the form

Code:
>1
akdfvcnciejcndmdjfk
>2
kdjkkkifjeeeeelfjfuf
>3
fjfhchdejhfhfhfhfhfhf
>4
skdkdhfhvnvncnccm

I think it would be possible with awk or sed, but not quite sure on how to implement it.
# 2  
Old 04-11-2013
Code:
$ awk '{ if ($0 == ">") { print $0 n + 1; n++ } else { print } }' file
>1
akdfvcnciejcndmdjfk
>2
kdjkkkifjeeeeelfjfuf
>3
fjfhchdejhfhfhfhfhfhf
>4
skdkdhfhvnvncnccm

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-11-2013
Code:
awk '/>/{++c;$0=$0 c}1' file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-11-2013
Thanks, you guys are the best.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add number prepend certain lines

from a(depends: ) bb(depends:lmbench ) cc(depends: ) CONFIG_DEFAULT1=y CONFIG_IOSCHED1=y CONFIG_CGROUP1=y ddd(depends: ) CONFIG_GENERIC_CPU1=y CONFIG_CRYPTO_AES_NI1=m to 1. a(depends: ) 2. bb(depends:lmbench ) 3. cc(depends: ) CONFIG_DEFAULT1=y ... (8 Replies)
Discussion started by: yanglei_fage
8 Replies

2. Shell Programming and Scripting

Get number of lines after certain string

Hello people, I have a file like that: start something 1 xxx yyy 2 zzz xxx 3 ccc vvv 4 mmm nnn stop something 1 aaa bbb 2 ccc ddd 3 eee fff 4 qqq www start something 1 rrr ttt 2 yyy uuu 3 iii ooo 4 ppp sss And I need only line starting with 1 and 3 and only after start... (7 Replies)
Discussion started by: apenkov
7 Replies

3. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

4. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

5. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

6. Shell Programming and Scripting

Reading n number of lines after finding string

I am trying to search a file for a value: "Top 30 reject reasons" and want the next 30 lines after that and output in a text file. If I knew the line number, I can use a combination of head and tail commands to get my results, but this doesn't seem to work when I don't have a line number. I... (2 Replies)
Discussion started by: oriqin
2 Replies

7. Shell Programming and Scripting

Add two lines in a single string

Dear All, I want to add two lines in single string. Example: String1: God Bless You. String2: Thank You. Now i want to store these two above lines into a single string(str) and when i will echo it, it should be like > echo $str God Bless You. Thank You. Please help me. Thanks in... (1 Reply)
Discussion started by: umesh.rout
1 Replies

8. UNIX for Dummies Questions & Answers

Number of lines containing a certain string

Hey I want to know if there is an option to know the number of lines containing a certain string (bit for example) in a file? Say I want to know number of lines containing only the string BIT in file xyz. I know how to get number of lines in a file by using wc -l but how do you get number of lines... (1 Reply)
Discussion started by: #moveon
1 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies
Login or Register to Ask a Question