Operating System - HP-UX
1838219 Members
3843 Online
110125 Solutions
New Discussion

insert text in the same line after a tab

 
SOLVED
Go to solution
Anand_30
Regular Advisor

insert text in the same line after a tab

Hi,

I have a file which has a single column. I need to enter some text on the same line after a tab. That is I need to create a second column based on some condition.

for example if my first column has a number '1234', I would enter 'abcd' in the same line delimited by a tab.

Can anyone please help me to accomplish this task.

Thanks,
Andy.
6 REPLIES 6
RAC_1
Honored Contributor

Re: insert text in the same line after a tab

something like this.

for i in `cat "your_file"`
do
if [ $i == "1234" ]
then
echo "$i abcd" >> /tmp/new_file
else
echo $i >> /tmp/new_file
fi

note in statement echo "$i abcd" >> /tmp/new_file, there is tab between $i and abcd.
There is no substitute to HARDWORK
RAC_1
Honored Contributor
Solution

Re: insert text in the same line after a tab

I just tried it myself. Few corrections.

for i in `cat "your_file"`
do
if [ $i = "1234" ]
then
echo "$i abcd" >> /tmp/new_file
else
echo $i >> /tmp/new_file
fi
cat /tmp/new_file|sort -u > /tmp/final.txt
done

note in statement echo "$i abcd" >> /tmp/new_file, there is tab between $i and abcd.
There is no substitute to HARDWORK
curt larson_1
Honored Contributor

Re: insert text in the same line after a tab

you could do

cat file | sed -e 's/^abc/& def/'

between & and def is your tab or ^I character

or

cat file | awk '/^abc/ {printf("%s\tdef\n",$0);next;}{print;}'
H.Merijn Brand (procura
Honored Contributor

Re: insert text in the same line after a tab

# perl -pe'BEGIN{%t=qw(1234 abcd 2345 defg 2536 gshe 5623 blah)}m/^\s*(\d+)\b/&&exists$t{$1}and s/\t/\t$t{$1}/' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor

Re: insert text in the same line after a tab

or

ex -s +"1,$ s/^abc/& def/ | wq" yourfile

Procura,
why not just,

perl -pe 's/^(1234)/$1\tabcd/;' fileName
H.Merijn Brand (procura
Honored Contributor

Re: insert text in the same line after a tab

curt, because he said "for example", and "based on some condition"

My solution entered 4 (random) conditions, but leaves it wide open to add many more without too much hassle

I like generic solutions based on the problem stated.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn