Hi,
I want to append each line in a file with string "foo foo" if number of charcters in the line are 4000. There can be n number of lines and this scenario applies to all lines. .. any takes? i want shell to do it.

-Seemit

Recommended Answers

All 5 Replies

Hey there,

Just FYI - it's good to put what you've tried in your post, so everyone doesn't think you're doing homework ;)

You can write this all on one line if you want - use expr to count the chars on each line of the file

#!/bin/sh

while read x
do
        chars=`expr "$x" : '.*'`
        if [ $chars -eq 4000 ]
        then
                echo "${x}foofoo" >>tmpfile
        else
                echo ${x} >>tmpfile
        fi
done <inputfile

cp inputfile inputfile.bak
mv tmpfile inputfile

Hope that helps :)

, Mike

This probably is bettre approach. Worked for me

while read line
do
if [[ ${#line} -eq 4000 ]]
         then echo ${line}| tee -a out.txt
else echo ${line}"foofoo"| tee -a out.txt
fi 
done < inputFile.txt

Thanks anyway Eggi.. and ermm.. was'nt doing homework.. its monkey work.

No offense meant,

I've just been warned against answering questions every once and a while when no proof of work was given in the post - just giving you a heads up since you had 0 posts :)

, Mike

No offense meant,

I've just been warned against answering questions every once and a while when no proof of work was given in the post - just giving you a heads up since you had 0 posts :)

, Mike

Thanks Mike. Will keep that in my script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.