Prefix a string to the contents of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prefix a string to the contents of a file
# 1  
Old 11-06-2008
Prefix a string to the contents of a file

Hi all,

I have a requirement where in i have to create a temporary file by prefixing a string of special characters to each row of a input file.


the input file is '|' delimited.

here is the content of input file
aaa|1234|axad|123
bbb|qwqw|qw|1334

the output should be

###|aaa|1234|axad|123
###|bbb|qwqw|qw|1334


i know how to do this using sed..but i just want to evaluate the options using awk. also i would like to know which one will be more performance effective if i have to process files containing 2-3 million records.

I am using K-shell.

Thanks,
Narendar
# 2  
Old 11-06-2008
Code:
time awk ' $0="###|" $0' inputfile
time sed 's/^.*$/###|&/' inputfile

time each one.
# 3  
Old 11-06-2008
Quote:
Originally Posted by jim mcnamara
Code:
time awk ' $0="###|" $0' inputfile
time sed 's/^.*$/###|&/' inputfile

time each one.
thank you for the response!!...
but i have to write the data to another file which might add up to
some more time.

also any idea on how to redirect the output of the awk to another
file??


Thanks,
Narendar
# 4  
Old 11-06-2008
Code:
time awk ' $0="###|" $0' inputfile > newfile
time sed 's/^.*$/###|&/' inputfile > newfile

# 5  
Old 11-07-2008
U can try this also!!!!!!!!!!!


awk '{print("###|",$0)}' filename > newfile

Cheers
# 6  
Old 11-07-2008
Quote:
Originally Posted by aajan
awk '{print("###|",$0)}' filename > newfile
Maybe you can explain why we need the parenthesis and comma?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to use file contents in replacement string

I want to replace a string by contents of file. I am trying the following sed command: cat sample | sed "s^<enter description here>^`cat details`^" But it is not working. a=`cat details` and using $a will not help since it will affect the whitespaces. What am I missing in the above sed... (5 Replies)
Discussion started by: anand_bh
5 Replies

2. Shell Programming and Scripting

Removing only Prefix string (!)

Hello everyone, I want to remove only prefix ME_ from all the values that are present in the FILEA. Below code I'm using for this. sed 's/ME\_//g' FILEA > FILEB Using the above code, all ME_ values are getting removed from the file. But the problem here is I want to remove only Prefix ME_... (4 Replies)
Discussion started by: ed_9
4 Replies

3. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

4. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

5. Shell Programming and Scripting

Using filename to determine a prefix that needs to be added to string column on file?

Possible filenames: CDD_Whatever.txt DDD_Whatever.txt If the file prefix = CDD, I'd like to prefix every person ID (second column in my examples below) on the file with "c-" If the file prefix = DDD, I'd like to prefix ever person ID with "d-" Input: Desired Output: Any help... (2 Replies)
Discussion started by: lrluis
2 Replies

6. UNIX for Dummies Questions & Answers

Replacing a particular string in all files in folder and file contents

I need to replace all filesnames in a folder as well as its content from AK6 to AK11. Eg Folder has files AK6-Create.xml, AK6-system.py etc.. the files names as well as contents should be changes to AK9-Create.xml, AK9-system.py etc All files are xml and python scripts. ---------- Post... (0 Replies)
Discussion started by: Candid247
0 Replies

7. 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

8. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 Replies

9. Shell Programming and Scripting

Replacing string in all instances (both filenames and file contents) in a directory

Hi, I have a set of files stored in a single directory that I use to set parameters for a physics code, and I would like to streamline the process of updating them all when I change a parameter. For instance, if the files are called A2000p300ini, A2000p300sub, A2000p300run, and the text in each... (3 Replies)
Discussion started by: BlueChris
3 Replies

10. Shell Programming and Scripting

How to cut prefix from a string?

Hi folks, I have the following parameter setting: export ADMIN_HOST_NAME=http://hostname.com I want to define a new parameter,ADMIN_HOST_NAME_NEW,which based on $ADMIN_HOST_NAME but I need to remove the prefix "http://". The requested result for $ADMIN_HOST_NAME_NEW is hostname.com How... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question

Featured Tech Videos