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
# 1  
Old 07-18-2009
put a string before a searched string

hi all!

i have a working that looks like this...

file1
MALE JOHN
MALE ANJO
FEMALE ANNE
MALE JAMES
FEMALE HONEY
FEMALE IZA

what i want to do is insert "M" when the first string is "MALE" and insert "F" when the first string is "FEMALE".

file1
M MALE JOHN
M MALE ANJO
F FEMALE ANNE
M MALE JAMES
F FEMALE HONEY
F FEMALE IZA

i tried awk:
Code:
 
if($1 == "MALE")
{print "M"" "$1" "$2}
else
{print "F"" "$1" "$2}


but is there any other one-liner code that would do the job? output should overwrite the working file. thanks!
# 2  
Old 07-18-2009
Something like this:

Code:
awk '{print substr($1,1,1) FS $0}' file

# 3  
Old 07-18-2009
@franklin: actually, it's not the first letter that should be inserted. im sorry if i mislead you, it's just an example. Thanks!
# 4  
Old 07-18-2009
Quote:
Originally Posted by kingpeejay
@franklin: actually, it's not the first letter that should be inserted. im sorry if i mislead you, it's just an example. Thanks!
Ok, then you should give a more elaborate example.

Regards
# 5  
Old 07-18-2009
for example: if the first string of a line is "MALE", then insert a string like "AM" before it.
# 6  
Old 07-18-2009
What are you trying to accomplish, can you give an explanation of your real world problem?

Regards
# 7  
Old 07-18-2009
i have this working file that i can't find all the values i needed. so, what i want to do is to put that missing value before the first string of each line.
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