0

I have one file Length.txt having multiples names (40) line by line. I want to write a small shell script where it will count the character count of each line of the file and if the count is less than 9 replace those lines with adding extra 8 spaces and 1 at the end of each line.

For example, if the name is

XXXXXX

replace as

XXXXXX        1

I tried with the below coding. It is working for me, however whenever it's replacing the line it is displaying all the lines at a time. So suppose I have 40 lines in Length.txt and out of that 4 lines having less than 9 character count then my output has 160 lines. Can anyone help me to display only 40 line output with the 4 changed lines?

#!/usr/bin/sh
#set -x

while read line;
do
count=`echo $line|wc -m`
        if [ $count -lt 9 ]
        then
        Number=`sed -n "/$line/=" Length.txt`;
        sed -e ""$Number"s/$line/$line       1/" Length4.txt
        fi
done < Length.txt
1
  • "Can anyone help me to display only 40 line output with the 4 changed lines?" Yes, we can help. Commented Sep 5, 2013 at 12:25

4 Answers 4

2

A single sed command can do that:

sed -E 's/^.{,8}$/&        1/' file

To modify the contents of the file add -i:

sed -iE 's/^.{,8}$/&        1/' file

Partial output:

94605320        1
105018263
2475218231
7728563        1
        1

* Fixed to include only add 8 spaces not 9, and include empty lines. If you don't want to process empty lines, use {1,8}.

Sign up to request clarification or add additional context in comments.

1 Comment

That's actually way more elegant, +1.
1
$ cat foo.input
I am longer than 9 characters
I am also longer than 9 characters
I am not
Another long line
short

$ while read line; do printf "$line"; (( ${#line} < 9 )) && printf "        1"; echo; done < foo.input
I am longer than 9 characters
I am also longer than 9 characters
I am not        1
Another long line
short        1

Comments

1

Let me show you what is wrong with your script. The only thing missing from your script is that you need to use sed -i to edit file and re-save it after making the replacement.

I'm assuming Length4.txt is just a copy of Length.txt?

I added sed -i to your script and it should work now:

cp Length.txt Length4.txt
while read line;
do
count=`echo $line|wc -m`
        if [ $count -lt 9 ]
        then
        Number=`sed -n "/$line/=" Length.txt`
        sed -ie ""$Number"s/$line/$line       1/" Length4.txt
        fi
done < Length.txt

However, you don't need sed or wc. You can simplify your script as follows:

while IFS= read -r line
do
    count=${#line}
    if (( count < 9 ))
    then
        echo "$line        1"
    else 
        echo "$line"
    fi
done < Length.txt > Length4.txt

2 Comments

+1 for actually pointing out the problem of the original snippet and not being as lazy as I am :-)
@user2642751 Looks like your version of sed doesn't support -i. You can use a temp file instead, like this: sed -e ""$Number"s/$line/$line 1/" Length4.txt > Length4.tmp && mv Length4.tmp Length4.txt. But I strongly suggest you use an alternative approach.
0
$ awk -v FS= 'NF<9{$0=sprintf("%s%*s1",$0,8,"")} 1' file
XXXXXX        1

Note how simple it would be to check for a number other than 9 characters and to print some sequence of blanks other than 8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.