how to add string inside the file's content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add string inside the file's content
# 1  
Old 07-13-2010
how to add string inside the file's content

Hi,

How to add string inside the file. In this case adding 63 in the beginning of each line. Thnks.

e.g. what is inside the file

Code:
Mobile Number Portability
 
9051151234
9051261345
9051393245
9051412345
9051507654

should like this as output:

Code:
Mobile Number Portability
 
639051151234
639051261345
639051393245
639051412345
639051507654

# 2  
Old 07-13-2010
Try something like this

sed 's/\(^[0-9]\)/63\1/' <file>
This User Gave Thanks to bankai For This Post:
# 3  
Old 07-13-2010
Alternatively,
Code:
sed '/^[0-9]/s/^/63/' infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-13-2010
The AWK way:
Code:
awk '/^[0-9]+/ { print "63"$0; next } { print }' infile

This User Gave Thanks to kevintse For This Post:
# 5  
Old 07-13-2010
it working

guys, its working and thanks you.Smilie

---------- Post updated at 01:05 PM ---------- Previous update was at 01:00 PM ----------

Hi,

can you share and describe what it does?

Thanks.
# 6  
Old 07-13-2010
Find all lines starting with a number, place that in buffer one. Then write 63 followed by buffer one

Quote:
sed 's/\(^[0-9]\)/63\1/' <file>
Find all lines starting with a number and replace the start of line with 63

Quote:
sed '/^[0-9]/s/^/63/' infile
Find all lines starting with one or more numbers, print 63 then the number
Quote:
awk '/^[0-9]+/ { print "63"$0; next } { print }' infile
This User Gave Thanks to bankai For This Post:
# 7  
Old 07-13-2010
Code:
#!/bin/bash

while read -r LINE
do
   case "$LINE" in
     [0-9]* ) LINE="63${LINE}"
   esac
   echo "$LINE"
done < "myfile"

This User Gave Thanks to kurumi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

2. Shell Programming and Scripting

How to save sorted content of a inside the same file?

Hi All, When i use sort Test, here is the output: $ sort Test a b b c d e f g h i But the contents in the file remain unsorted, how to do that? ]$ cat Test g i (6 Replies)
Discussion started by: chandrakanth
6 Replies

3. Shell Programming and Scripting

Replace string in a file with some content indexed from another file.

We have two files file 1: (usually small, ~100 lines), each line contains a : separated index, value e.g 2: Apple 1: Banana 5: Pear 7: Orange File 2: (usually large, 10 million lines or more), each line contains a single string value. e.g xyz1 xyz2 xyz3 xyz4 xyz5 xyz6 xyz7 Now... (2 Replies)
Discussion started by: AlokKumbhare
2 Replies

4. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

5. Shell Programming and Scripting

Replace string by file content

hi I have template file my.tpl: bla-bla-bla <link href="style.css" type="text/css"> bla-bla-bla and style.css : body{margin: 0px;} I want to get in result one file: bla-bla-bla <script>body{margin: 0px;}</script> bla-bla-bla I tryed to used SED: sed '/<link .*href=\"(*)*\"... (6 Replies)
Discussion started by: dim_nsk
6 Replies

6. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

7. UNIX for Dummies Questions & Answers

How to find a file with specific string inside it.

Hi , Is there any way i can find a file with specific word inside it. For example if i want to find a file which has some text written inside it. How would i form a command to search them? (3 Replies)
Discussion started by: pinga123
3 Replies

8. Shell Programming and Scripting

Search for string in filename, not file content

How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test". (3 Replies)
Discussion started by: daflore
3 Replies

9. UNIX for Dummies Questions & Answers

Search and Parse string from inside a File

Hello, I barely know the basics, but I am very determined to learn. I want to parse a few characters from each row, use that string to search another file and display the line number where I found the value in the file. I don't know if this can all be done on the command line, so I am creating a... (2 Replies)
Discussion started by: SSims
2 Replies

10. Shell Programming and Scripting

replace a string with content from another file

Hi, I'm a newbi in shell script. Here what I want to do: FileA: bor bor bor xxxx bib bib bi FileB: something something something I want to replace string "xxxx" in FileA with contents of FileB. i tried with sed: fileb=`cat FileB` reg=xxxx file=FileA (4 Replies)
Discussion started by: afatguy
4 Replies
Login or Register to Ask a Question