Numbering a Text File

Running into a little problem with blank lines.

My file is of this format:

To number each line of the file i would use:

 
n=1
echo "$FILE" |
while read line
do
echo "$n) $line"
n=`expr $n + 1`

But really, i dont want to number the blank lines.
What i've tried is to use sed commands to the FILE before i pipe its contents into the while, but sed just bunchs the contents together and my code reads it at one line..

The sed command sed 'nd' reads every other line right? Unfortuantely for me that doesnt work either..

For simplicity you might want to try the cat command.

cat -b filename

or if you want to stick with your code just add the command before the while command.

grep -v '^$' filename

to delete all blank lines.

or

grep -v '^$' filename |cat -b

it may however depends of how large your file is. Hope this helps.

Try this awk solution:

awk 'NF{$0=++c FS $0}1'  file

cat file.txt | sed '/^$/d' | cat -n

sed '/./!d' file | sed =

Can i store the contents of the file into a varaible and still be able to delete blank lines? Thats what i've been trying

Well, what im after is to use awk or sed ( awk might be easiest ) to print each line of the file sepearately.

awk $NF1 ( for line 1 )
awk $NF2 ( for line 2 )

i dont know the syntax for it..

The plain text that i posted above is similar to the contents of the file that im working with. Is there an awk command to do the following:

awk ( echo something ) $myvar print the line 1 of the contents of the varaible..

Im sorry, im so bad with unix.

In that case just modify your script to validate the string.

line_no=1
while read each_line
do
if [ ! -z $each_line ]
then
line_no=`expr $line_no + 1`
echo "$line_no $each_line"
fi
done < input_file > output_file

Eureka.

U guys make things way to complecated. hehe. i just used sed -n '1p' for the first line..
But im wondering..Is there a way to loop through so i dont have to use a different varaible for a new line each time..? Because my sed pipeline is kinda long..

Example

Var1=$( cat $FILE | sed ..... | sed -n '1p' )
Var2=$( cat $FILE | sed ..... | sed -n '2p' )

OR can i have the long sed pipeline to be a varaible..To then have:
Var3=$( cat $FILE | longVar | sed -n '3p' )

Also..A command that would be really extemely soo very valuble to me would be to print lines 1 - 12 or so at once.

sed -n '/[1-12]/'
or something like that, because if i were to have sed -n '1-12p' it wouldnt include the blank lines..understand? I guess i have no choice to do it seperately. grr.

new line after X'th feild? in awk or sed or grep or ..

Hi! If I were you, I would try the suggested steps to strip off the blank lines then from there you can make further actions on your file.

I guess your file is not that big so why not paste it here with code tags. Paste your exact input file(if that's possible) and then your expected output file, just to make things clear.

i might get in trouble if i post the actual text file and also the sed pipeline.
I think i have it figured out tho..And i dont want to quit until its done, so this is an all nighter for me..But as of this moment i've already done what i said i was going to do and i have a consecutive amount of varaibles. And it works for 1 of my text files! But i need to get it to work for all of them and i think all i have to do is delete a space from the files LOL. Its like im globally reading 1 file for all the files i need. So everything is changed if i change 1 thing.

Thanks for your help!

wk $NF1 ( for line 1 )

try this (*) out :

n=1
cat "$FILE" |
while read line
do
    if [ "$line" == "" ]
    then
        echo
    else
        echo "$n) $line"
        n=`expr $n + 1`
    fi
done

(*) but be aware that "echoing" a line read from a file crunches spaces, besides some other side effects ;

good luck, and success !

botao