put a string before a searched string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting put a string before a searched string
# 8  
Old 07-18-2009
Quote:
Originally Posted by kingpeejay
for example: if the first string of a line is "MALE", then insert a string like "AM" before it.
Assuming you mean the first word of the line:

Code:
awk '/^MALE/{print "AM" $0}' file



---------- Post updated at 16:54 ---------- Previous update was at 16:36 ----------

Or maybe you mean something like:

Code:
awk '/^MALE/{$0="AM" $0}1' file

Regards
# 9  
Old 07-18-2009
Here is example how to make n rules.
Code:
#!/bin/sh
file="file1"
cp $file $file.bak
tf=$$.tmp
awk ' 
# rule and print 
/^MALE / { print "M",$0  ; next  }
/^FEMALE / { print "F",$0 ; next}
# default
{ print "?",$0 }
' $file > $tf
# overwrite original file
cat $tf > $file
rm -f $tf


Last edited by kshji; 07-19-2009 at 03:41 AM..
# 10  
Old 07-18-2009
Trythis:

Code:
echo 'MALE JOHN' | sed 's/^MALE /M MALE /; s/^FEMALE /F FEMALE /;'

Output:
Code:
M MALE JOHN

You can add more substitutions as you want.
Just like this "'s/^THIS THING /TT THIS THING /; "
# 11  
Old 07-20-2009
shell:

Code:
sed 's/^\([A-Z]\)/\1 \1/'

perl:

Code:
perl -ne '{s/^([A-Z])/(($1 eq "M")?"M ":($1 eq "F")?"F ":"O ").$1/e;print;}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below. I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found. addthisline="I need to be inserted after the comments" Thus my searchstring is... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Put a string to the beginning of a file without a linefeed

Hello, I need to put the following string to the beginning of a file - but it should not create a newline at the end of the string. So, the file I have is a compressed one - with gzip. And I would like to add an ASCII-String to the beginning of the file. The string has a length of 69... (5 Replies)
Discussion started by: API
5 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Programming

How to put dot(.) in a string in C?

Hi all, Can anybody please tell me how can I put dot(.) in a string using C programming. for example -- I am having string "10111988" and I want to convert it as "10.11.1988" I will appriciate and thanks in advance.. (4 Replies)
Discussion started by: smartgupta
4 Replies

5. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

6. Shell Programming and Scripting

put string end of the line

I've a problem to put .h end of the line..below my input file fg_a bb fg_b bb fg_c bb fg_d aa fg_f ee and i want the output file as below fg_a.h bb fg_b.h bb fg_c.h bb fg_d.h (6 Replies)
Discussion started by: zulabc
6 Replies

7. Shell Programming and Scripting

How to compare particular string, if it is equal put into a new file

Hi, I have a file (sample.txt) this file contains below text. ./au ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./po ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./xxsbx/11.5.0/java/src ./xxsbx/11.5.0/lib ./xxsel ./xxsel/11.5.0 ./xxsel/11.5.0/admin ./zfa ./zfa/11.5.0... (2 Replies)
Discussion started by: gagan4599
2 Replies

8. Shell Programming and Scripting

greping last occurrence of the searched string

Hello, I have active log file i want to grep the last occurrence of the word in that log file the log file gets on increasing and increasing i want to fetch it from live file. Please guide me, Thanks in advance (4 Replies)
Discussion started by: vidurmittal
4 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

10. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies
Login or Register to Ask a Question