I have been working on a script all day and am having very little success. Essentially, I am reading a file and trying to format the data in the file. I am currently trying to put the data into three separate files, so that I can paste them back together into a single output file.
Trying to use the for and while loops are both giving me problems. The while loop only creates 1 file and leaves the other two blank. The for loop keeps giving me an error and yet it still presents my data to stdout, but not the output files. Here are the two methods I am trying to use.
exec < workunits-status.txt
while read line; do
cut -c 1-12 | sed "s/ //g" >> tempfile1.txt
cut -c 13-35 | sed "s/ //g" >> tempfile2.txt
cut -c 36 >> tempfile3.txt
done
paste tempfile1.txt tempfile2.txt tempfile3.txt >> workunits.txt
for i in `cat workunits-status.txt`
do
cut -c 1-12 $i | sed "s/ //g" >> tempfile1.txt
cut -c 13-35i $i | sed "s/ //g" >> tempfile2.txt
cut -c 36 $i >> tempfile3.txt
done
The while loop gives me tempfile1.txt with data, but not in the order of the workunits-status.txt file and does not create the other two files.
The for loop gives me the error below, yet it is showing the data that I am going after.
cut: cannot open : 1458
cut: bad list for c option
cut: cannot open : 1458
cut: cannot open : CustomActivity1
cut: bad list for c option
cut: cannot open : CustomActivity1
cut: cannot open : 3
cut: bad list for c option
cut: cannot open : 3
cut: cannot open : 1459
cut: bad list for c option
cut: cannot open : 1459
cut: cannot open : CustomActivity1
cut: bad list for c option
cut: cannot open : CustomActivity1
cut: cannot open : 3
cut: bad list for c option
cut: cannot open : 3
Can someone please help and explain why both of these are not working? Your help would be greatly appreciated. Please assume that all files are in the current working directory of the script. Also, only shell scripting. I am not experienced in Perl, expect, tcl or other languages. The use of sed and awk are ok.
My input file has three fields that I cannot format using the application utility that creates it. I have to format it afterwards. I need to break the three fields apart so that I can output it to a report that has three columns. The data fields should align under each column.
I initially tried to use the following syntax, but could not get it to work either:
exec < workunits-status.txt
while read line; do
if [[ $F1 -lt 100 ]];
then
awk -v F1="$FIELD1" F2="$FIELD2" F3="$FIELD3" '{print $F1 " " $F2 " " $F3}' >> workunits.txt
elif [[ $F1 -gt 99 && $F1 -lt 1000 ]];
then
awk F1="$FIELD1" F2="$FIELD2" F3="$FIELD3" '{print $F1 " " $F2 " " $F3}' >> workunits.txt
else
awk F1="$FIELD1" F2="$FIELD2" F3="$FIELD3" '{print $F1 " " $F2 " " $F3}' >> workunits.txt
fi
done
Where FIELD1, FIELD2, and FIELD3 are variables declared at the beginning of the script using the cut command as seen earlier in the two loop examples. And FIELD1 will be a numeric value ranging from 1 - 9999.