How can I replace the lines that start with a star and replace it with numbers start from 1?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I replace the lines that start with a star and replace it with numbers start from 1?
# 1  
Old 11-07-2014
how can I use sed to replace the lists that begins with (*) in a file

I need to replace the (*) in the fist of a list with numbers using sed for example >

this file contain a list

* linux

* computers

* labs

* questions

to >>>>

this file contain a list

1. linux
2. computers
3. labs
4. questions

I tried using

sed -e 's/*//' file.in > file.out | sed -i = file.out

Last edited by aalbazie; 11-07-2014 at 02:09 PM.. Reason: mistake
# 2  
Old 11-07-2014
You better escape the special character \*
sed is not the right tool for this. The = command is mainly for debugging.
Of course - everything is possible
Code:
sed = list | sed 'N;s/\n\*//'

awk or the shell's while loop do this better. With awk:
Code:
awk '{$1=NR; print}' list

Shell:
Code:
i=0; while read first other; do echo "$((i+=1)) $other"; done < list


Last edited by MadeInGermany; 11-07-2014 at 02:39 PM..
# 3  
Old 11-07-2014
Quote:
Originally Posted by MadeInGermany
You better escape the special character \*
sed is not the right tool for this. The = command is mainly for debugging.
Of course - everything is possible
Code:
sed = list | sed 'N;s/\n\*//'

awk or the shell's while loop do this better. With awk:
Code:
awk '{print NR,$2}' list

thank you for your replay this didn't work the file that I want to do this on is more than 400 lines and there are about 30 lines starts with star when I tried your code I did
sed = file.in > list.out | sed 'N;s/\n\*//' list.out
it id number all the lines in the file
# 4  
Old 11-07-2014
Please in your posts wrap your code in [code] tags!
This should also solve your new requirement; I have predicted your next requirement: reset the counter outside the * list.
Code:
awk '{if ($1=="*") {$1=++i} else {i=0}; print}' list

# 5  
Old 11-07-2014
How about:

Code:
C=1
NF=file_new.txt

[[ -f $NF ]] && rm -fr $NF
touch $NF

while read line;do
	[[ "*" = "${line:0:1}" ]] && \
		line="${line/\*/$C.}" && \
		C=$((C+1)) ||  \
		C=1
	echo "$line" >> $NF
done<file.txt

cat $NF

Hope this helps

Last edited by sea; 11-07-2014 at 02:55 PM.. Reason: Code update, a little smaller
# 6  
Old 11-10-2014
@sea: nothing speaks against
Code:
C=1
while read line; do
  ...
  echo "$line"
done <file.txt >file_new.txt

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 11-10-2014
@ MadeinGermany: I'm not used to 'double' redirections as yours, wouldnt this overwrite the file_new.txt with just the 'most recent' (as in the current line-) output?
Or would i have to understand (technical incorrect i know) such a command (the behaviour) like cat?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String at the start of each line

Replace String at the start of each line (3 Replies)
Discussion started by: Mahesh_RPM
3 Replies

2. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

3. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

4. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: anshaa
7 Replies

5. UNIX for Advanced & Expert Users

Replace a value in the file that start with quotes

I have to replace the specific encypted value in the file which is will be a taken as input from user always.PADWD appears two times in the document of which I need to replace the line which start with double quotes. Please help. "PADWD"\1\"LL9@99V@N=3M0O4I4;J43KH6"---Sample line in the file. ... (6 Replies)
Discussion started by: chandu123
6 Replies

6. Shell Programming and Scripting

sed&awk: replace lines with counting numbers

Dear board, (I am trying to post this the 3rd time, seems there's some conflicts with my firefox with this forum, now use IE) ------ yes, I have searched the forum, but seems my ? is too complicated. ------------origianl file --------------- \storage\qweq\ertert\ertert\3452\&234\test.rec... (4 Replies)
Discussion started by: oUo
4 Replies

7. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

8. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

9. Shell Programming and Scripting

Start program in background (or start crontab ahead of time)

Hey! I'm working on a script that will add a user, create some configfiles, and add a crontab for the user. The crontab looks like the following: @reboot /home/user/program config.conf & I would like for this process to start at the end of my script under the corresponding username by... (0 Replies)
Discussion started by: noratx
0 Replies

10. UNIX for Dummies Questions & Answers

inserting uniq sequential numbers at the start of the file

Hi Unix gurus, I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number. Example: File is as follows |123|4test|test |121|2test|test |x12|1test|test |vd123|5test|test starting number is : 120 ... (7 Replies)
Discussion started by: jingi1234
7 Replies
Login or Register to Ask a Question