# put a string before a searched string

**URL:** <https://community.unix.com/t/put-a-string-before-a-searched-string/244903>\
**Category:** Shell Programming and Scripting\
**Created:** [July 18, 2009, 9:55am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903 "2009-07-18T09:55:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingpeejay](https://community.unix.com/letter_avatar/kingpeejay/32/6_449c533f586939b6e5f246632a46421f.png) [@kingpeejay](https://community.unix.com/u/kingpeejay)\
**Post date:** [July 18, 2009, 9:55am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/1 "2009-07-18T09:55:18Z")

</div>

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:

```nohighlight
 
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!

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 18, 2009, 10:02am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/2 "2009-07-18T10:02:14Z")

</div>

Something like this:

```nohighlight
awk '{print substr($1,1,1) FS $0}' file

```

---

<div class="post-metadata">

**Author:** ![kingpeejay](https://community.unix.com/letter_avatar/kingpeejay/32/6_449c533f586939b6e5f246632a46421f.png) [@kingpeejay](https://community.unix.com/u/kingpeejay)\
**Post date:** [July 18, 2009, 10:05am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/3 "2009-07-18T10:05:33Z")

</div>

@franklin: actually, it's not the first letter that should be inserted. im sorry if i mislead you, it's just an example. Thanks!

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 18, 2009, 10:12am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/4 "2009-07-18T10:12:50Z")

</div>

> [@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

---

<div class="post-metadata">

**Author:** ![kingpeejay](https://community.unix.com/letter_avatar/kingpeejay/32/6_449c533f586939b6e5f246632a46421f.png) [@kingpeejay](https://community.unix.com/u/kingpeejay)\
**Post date:** [July 18, 2009, 10:16am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/5 "2009-07-18T10:16:59Z")

</div>

for example: if the first string of a line is "MALE", then insert a string like "AM" before it.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 18, 2009, 10:21am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/6 "2009-07-18T10:21:01Z")

</div>

What are you trying to accomplish, can you give an explanation of your real world problem?

Regards

---

<div class="post-metadata">

**Author:** ![kingpeejay](https://community.unix.com/letter_avatar/kingpeejay/32/6_449c533f586939b6e5f246632a46421f.png) [@kingpeejay](https://community.unix.com/u/kingpeejay)\
**Post date:** [July 18, 2009, 10:26am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/7 "2009-07-18T10:26:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 18, 2009, 10:54am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/8 "2009-07-18T10:54:44Z")

</div>

> [@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:

```plaintext
awk '/^MALE/{print "AM" $0}' file

```

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

Or maybe you mean something like:

```plaintext
awk '/^MALE/{$0="AM" $0}1' file

```

Regards

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [July 18, 2009, 11:13am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/9 "2009-07-18T11:13:30Z")

</div>

Here is example how to make n rules.

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![edidataguy](https://community.unix.com/letter_avatar/edidataguy/32/6_449c533f586939b6e5f246632a46421f.png) [@edidataguy](https://community.unix.com/u/edidataguy)\
**Post date:** [July 18, 2009, 6:37pm UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/10 "2009-07-18T18:37:05Z")

</div>

Trythis:

```nohighlight
echo 'MALE JOHN' | sed 's/^MALE /M MALE /; s/^FEMALE /F FEMALE /;'

```

Output:

```nohighlight
M MALE JOHN

```

You can add more substitutions as you want.  
Just like this "'s/^THIS THING /TT THIS THING /; "

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/6_449c533f586939b6e5f246632a46421f.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [July 20, 2009, 12:52am UTC](https://community.unix.com/t/put-a-string-before-a-searched-string/244903/11 "2009-07-20T00:52:56Z")

</div>

shell:

```nohighlight
sed 's/^\([A-Z]\)/\1 \1/'

```

perl:

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

```
