Add word/value at the beginning of each line in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add word/value at the beginning of each line in a file
# 1  
Old 11-30-2012
Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ?

i have file number.txt and the output is below

1000
1001
1002
1003
1004

i want to add 000 at the beginning of each line, desire output is below
0001000
0001001
0001002
0001003
0001004
and so on

please advise how to do it, i been searching forum but with no luck

---------- Post updated at 10:40 PM ---------- Previous update was at 10:33 PM ----------

Quote:
Originally Posted by jason6247
how to add value/word at the beginning of each line in a file ?

i have file number.txt and the output is below

1000
1001
1002
1003
1004

i want to add 000 at the beginning of each line, desire output is below
0001000
0001001
0001002
0001003
0001004
and so on

please advise how to do it, i been searching forum but with no luck
i have tried command seq -f "%010g" first number last number
but seq can only format number until 6 digit long, it wont print output 0000100001 0000100002 instead it become 0000.5.+.09 something
# 2  
Old 11-30-2012
Will this work for you?
Code:
awk ' { print "000" $0; } ' number.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-01-2012
Use awk:
Code:
awk '{printf("%015d\n",$1)}' number.txt


Last edited by mirni; 12-01-2012 at 12:52 AM..
# 4  
Old 12-01-2012
Have a look at this threat !

Quote:
Originally Posted by jason6247
how to add value/word at the beginning of each line in a file ?

i have file number.txt and the output is below

1000
1001
1002
1003
1004

i want to add 000 at the beginning of each line, desire output is below
0001000
0001001
0001002
0001003
0001004
and so on

please advise how to do it, i been searching forum but with no luck

---------- Post updated at 10:40 PM ---------- Previous update was at 10:33 PM ----------



i have tried command seq -f "%010g" first number last number
but seq can only format number until 6 digit long, it wont print output 0000100001 0000100002 instead it become 0000.5.+.09 something
Have a look at :
https://www.unix.com/unix-dummies-que...es-column.html
Regards
# 5  
Old 12-01-2012
Through Sed..
Code:
sed 's/^/000/' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 12-07-2012
Quote:
Originally Posted by michaelrozar17
Through Sed..
Code:
sed 's/^/000/' inputfile

WOOHOO.. Thanks GUYS. This code works just fine. I really appreciate all your help Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies

2. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

3. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

4. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

5. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

6. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

7. Linux

Add file's date at beginning of every line in file

How would I do that? /Rutger (6 Replies)
Discussion started by: rutgerblom
6 Replies

8. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

9. Shell Programming and Scripting

Want to add a word in front a of each line of a file

Hi, Can anybody help me how to add a word in front of a line in a file.Actually it is bit tricky to add a word. i will give a sample for this: Input : 1110001 ABC DEF 1110001 EFG HIJ 1110001 KLM NOP 1110002 QRS RST 1110002 UVW XYZ Output: %HD% 1110001 ABC DEF %DT% 1110001 EFG HIJ... (4 Replies)
Discussion started by: apjneeraj
4 Replies

10. Shell Programming and Scripting

Add a word at the end of each line in a file

Hi I want to read a flat file and add a word/value at the end of each line in the file and store the output in a temporary file. How can i do this? Plz help me with this. Regards, Saurabh (6 Replies)
Discussion started by: bhalotias
6 Replies
Login or Register to Ask a Question