problem developing for or while loops with the cut command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem developing for or while loops with the cut command
# 1  
Old 11-11-2008
problem developing for or while loops with the cut command

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.
# 2  
Old 11-12-2008
Hi,
There are some strange things going on in Your code; why do You want to exec the txt-file? In the first while loop the cut statement has no argument, in You first for loop the i is set to every word (not line) in the txt-file. And so on...

Could You give us a few lines from the text file that You want to process and an example of desired output?

/Lakris
# 3  
Old 11-12-2008
Try this:

Code:
while read line; do
    echo "$line" | cut -c 1-12 | sed "s/ //g" >> tempfile1.txt
    echo "$line" | cut -c 13-35 | sed "s/ //g" >> tempfile2.txt
    echo "$line" | cut -c 36 >> tempfile3.txt
done <  workunits-status.txt
paste tempfile1.txt tempfile2.txt tempfile3.txt >> workunits.txt

The problem was that the first cut was reading the same standard input source as the while loop and consuming all of the input, leaving nothing for the remaining commands to read. Since you need to use the input line for all 3 cut commands you just need to use the contents stored in the line variable and send it to each individual cut.

Lakris, the exec < workunits-status.txt doesn't execute the text file, it assigns it as the default source of standard input.

A simpler awk solution would be something like:

Code:
awk '{print substr($0,1,12), substr($0,13,22), substr($0,36)}' workunits-status.txt > workunits.txt

# 4  
Old 11-12-2008
Getting closer

Thanks for the feedback. Still not doing quite what I need. Here is a sample of the input. The first column has 12 characters with leading spaces. The last column is character #36.

Code:
           4 pftestAssert02b       3
          14 pftestCustom01b       3
          28 pftestAssert02b       3
          38 pftestCustom01b       3
         107 pftestAssert02b       3
         117 pftestCustom01b       3
         129 pftestAssert02b       3
         139 pftestCustom01b       3
        1043 VMOHMQTASK            3
        1044 VMOHMQTASK            3
        1045 VMOHMQTASK            3
        1073 email_test            3

Here is the expected output:

Code:
WorkUnits:             Process-Name:              Status:
4                      pftestAssert02b            3
14                     pftestCustom01b            3
28                     pftestAssert02b            3
38                     pftestCustom01b            3
107                    pftestAssert02b            3
117                    pftestCustom01b            3
129                    pftestAssert02b            3
139                    pftestCustom01b            3
1043                   VMOHMQTASK                 3
1044                   VMOHMQTASK                 3
1045                   VMOHMQTASK                 3
1073                   email_test                 3

The suggested awk script is getting me closer, however that is why I was using the while loop, so that when the workunit was less than 100, it would put in the two extra spaces needed in the output. When the number was between 100 -199, it would put in the extra space. The awk needs to remove leading spaces as well, which is what my sed after the cut was trying to accomplish.

Once again, any help would be greatly appreciated.

Last edited by synergy_texas; 11-12-2008 at 07:12 PM..
# 5  
Old 11-12-2008
Sorry. The expected output has the columns lined up. The forum seems to have taken out the spaces.

This has been edited now with the [code] tags in this posting. The output should look correct.

Last edited by synergy_texas; 11-12-2008 at 07:15 PM..
# 6  
Old 11-12-2008
Working now, for the most part. I have the columns aligning up as I would like. The only other thing I would like to do is trim the leading spaces out of the first field. I am using the awk script as suggested before. The only thing I added was to put spaces between the three fields. This essentially lines them up into separate columns as I need.

Now I just want to trim off the leading spaces of the first field so that the numbers show up at the beginning of the column and not at the end.
# 7  
Old 11-12-2008
Quote:
Originally Posted by synergy_texas
Sorry. The expected output has the columns lined up. The forum seems to have taken out the spaces.
Edit your previous post and use [code]..[ /code] tags when you post data or code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

2. Shell Programming and Scripting

Problem in using cut command with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like.................. LSCRM(Application Name): 1: This is my first application. 2: Today we did shell scripting automation for this app. 3: It was really a good fun in doing so. 4: Really good.| (Here i... (7 Replies)
Discussion started by: Abhijeet Anand
7 Replies

3. Shell Programming and Scripting

Problem in using cut command

Hi Friend , i have one file say xyz.lst and it has content like dn: cn=m.hariharan,cn=employee,cn=delhi circle,cn=users,dc=industowers,dc=c dn: cn=ajay.jain,cn=employee,cn=gujarat circle,cn=users,dc=industowers,dc=com dn: cn=ajitkumar.thakor,cn=employee,cn=gujarat... (4 Replies)
Discussion started by: harish anand
4 Replies

4. Shell Programming and Scripting

Problem with cut command

Hi! I get a md5 hash of a file with this command: openssl md5 /Users/me/MyLogo.png | cut -f 2 -d ' ' "cut" because I just want the hash. But there is a problem -> that doen't work with a path with spaces: openssl md5 /Users/me/MyLogo 2.png | cut -f 2 -d ' ' The result is "2.png)=" ...... (1 Reply)
Discussion started by: Dirk Einecke
1 Replies

5. Shell Programming and Scripting

CUT command problem

Hi I have a strange problem when using cut command when i am using the below command, it is working fine,I am getting the data in new file xyz.dat cut -c 1-75 abc.dat > xyz.dat when i am using the below command, I am getting the data in new file abc.dat , but empty file cut -c 1-75... (4 Replies)
Discussion started by: vaas
4 Replies

6. Shell Programming and Scripting

Problem using cut command in shell script

I'm new to shell programming, and am having a problem in a (Korn) shell program, which boils down to this: The program reads a record from an input file and then uses a series of "cut" commands to break the record into parts and assign the parts to variables. There are no delimiters in the... (2 Replies)
Discussion started by: joroca
2 Replies

7. Shell Programming and Scripting

Problem with cut command

I am trying to take one part of my text from file and save it to variable $x I tryed this... x=`cut -c 6-9 $fajl` my file looks like this fajl: 21890001277 89386911 23638FBCDC 28EE01A1 0000 26855 124 244326 21890001277 89766911 23638FBCDC 28E021A1 0000 26557 134 684326 21890001277... (7 Replies)
Discussion started by: amon
7 Replies

8. UNIX for Dummies Questions & Answers

facing problem with cut command

hi , i used ls -ltr | cut -f 1 > \dev\tty but all teh coulmns r getting printed instead of only one........how can i resolve this? prob 2 : wud be able start cutting from last field......supposing in the case of dyanmic list.i dunno the field number of last column.......so is... (3 Replies)
Discussion started by: vivekshankar
3 Replies

9. Shell Programming and Scripting

Problem with UNIX cut command

#!/usr/bin/bash cat /etc/passwd | while read A do USER=`echo “$A” | cut -f 1 -d “:”` echo “Found $USER” done This shell script should make USER = the first field of the first line of the file /etc/passwd Eg: adm daemon bob jane kev etc ... However USER=echo... (3 Replies)
Discussion started by: kevin80
3 Replies

10. UNIX for Dummies Questions & Answers

Cut command problem !

Hi all! Here is my problem : $ more file yougli:passwd:123456:3265:Yepa Yepo:/home/yougli:/bin/ksh As you can see, in the field "information", there are two spaces between "Yepa" and "yepo". My problem is : $ PARAM='more file | cut -d":" -f5' $ echo $PARAM Yepa Yepo Now i only... (2 Replies)
Discussion started by: tomapam
2 Replies
Login or Register to Ask a Question