Link to home
Start Free TrialLog in
Avatar of jameswalt
jameswalt

asked on

Bash script to parse line by line

I have a file with the following format:
d    28854           cpmnt    open     username  owner  3   062 description of problem
d    28855           cpmnt    open     username  owner  3   062 description of problem
d    28859_d           cpmnt    open     username  owner  3   062 description of problem


I need to "grab" the second token of every line, no matter how large the token is. The 2nd token is characterized by whitespace delimerters on both sides. (generally spaces, but I would like to be able to handle tabs as well).
I would like generate a new file and insert onto each line the 2nd token from this first file.
This is a fun programming excercise. I have been working on it for a little while now, but just do not know the commands wlel enough to be able to get it where i need it to be.

The faster the response on this, the more points I will add to the question, and the better grade I will offer. Please help!

Here is the relevant portion of the script that I have so far (not a big deal at all)
It simply reads in the file line by line and outputs it to the terminal

# START OF SCRIPT
exec < currentDefectsLog
while read line
do
echo $line
done
#END OF SCRIPT
Avatar of PaulS_III
PaulS_III

try:

echo $line | awk '{ print $2 }'

That should work for ya


Paul
Avatar of jameswalt

ASKER

That works nicely!

Any idea how to make it so that it starts reading/printing on the third line?
also, have it so that it stops on the 2nd to last line?

Ill up the points if you can help me with these.
Don't know that this is the best way but you could set an accumulator and increment it

x=1

exec < currentDefectsLog
while read line
do

if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi

x=(($x + 1))
done

that will get you to start at line 2

Then to stop at the second line from the end we can do this

filelinecount=` wc - l | cat currentDefectsLog`

then inside the while we can do this

exec < currentDefectsLog
while read line
do

if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi

if [ $x -eq (($filelinecount - 2)) ]
then
exit 0
fi

x=(($x + 1))

done

put it all together and I think that will take care of it.

Let me know if you require additional help.

Paul
i have not tried those out yet.. but how do I say:

"if a directory does NOT exist then do whatever" ?

ive tried

if [ ! -d directory]
then
  run some code
fi


but that doesnt work.
ASKER CERTIFIED SOLUTION
Avatar of PaulS_III
PaulS_III

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
n/m i got it
Thank you for your continuous help. I will add more points as soon as soon as a refund comes in from an unanswered question.

How would I make the script do the following:

read a file line by line where each line is a single token of alphanumeric characters, and "_" characters
  **trim white spaces from line**
  if line is NOT blank then
    if directory /defects/$line exists then
      delete /defects/$line
    fi
  fi
done

I tried doing this:

#---start of script

while read line
do
        line= echo $line | awk '{print $0}'
        if [ -n $line ]
        then
                if [ -d "/defects/$line" ]
                then
                        rm /defects/$line -f -r
                fi
        fi
done

#--- end of script

it almost works - except one thing. if it encounters a line with just whitespace on it, it will delete the /defects directory - when I never want that deleted as it is. i want to skip all blank lines.
ill have a shorter one for you

#!/bin/bash
#start
#Usage ./scriptname <file>

#Gives you the total number of lines in the file
nrow=`wc -l $1 | awk '{print $1}'`
#Nr or rows to tail the file with all rows - 3
rowtail=$(($nrow - 3))
#Nr or rows to head the file with all rows - 2
rowhead=$(($rowtail - 2))

#Output the right fields
tail -n $rowtail $1 | head -n $rowhead | awk '{ print $2 }'

#end

/Rob