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

Dani AI

Generated

Both and solved this with shell read loops. That works for small files, but there are a few practical pitfalls to watch for: shell read can mangle leading/trailing whitespace and backslashes unless used as IFS= read -r, loops in shell are much slower than stream tools for large files, and counting "characters" vs bytes matters for multibyte (UTF‑8) data.

A compact, robust streaming approach is awk (fast, handles long lines better than a shell loop). For plain text where counting characters in the current locale is fine:

awk 'length($0) == 4000 { print; next } { print $0 "foo foo" }' input.txt > output.txt

length($0) counts characters in the current locale; $0 preserves leading/trailing spaces. See the gawk manual for string/length behavior: gawk manual.

If you want in-place editing and a backup, Perl is convenient and gives precise control (and can be made Unicode-aware):

perl -i.bak -pe 'chomp; $_ .= "foo foo\n" unless length($_) == 4000' file.txt

This creates file.txt.bak first; adjust encoding handling if you need true Unicode codepoint counts. See Perl run options: perlrun.

Quick checklist missed in the thread:

  • If you keep the shell loop, use IFS= read -r line to preserve whitespace/backslashes.
  • Avoid unquoted echo $var (use printf '%s\n' "$var" or the streaming tools above).
  • Test on a sample and keep a backup before overwriting the original file.
  • Decide whether you need byte counts or character counts (multibyte encodings require locale/Unicode-aware tools).

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.