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
# 8  
Old 11-12-2008
Almost there

OK. So here is what I have. It almost looks good, but can't figure out why it is giving me the output it is providing.

Code:
exec < workunits-status.txt
while read line; do
    awk '{ if ($1 < 10) print substr($0,12), "       ", substr($0,14,35), "
   ", substr($0,36);
          else if (($1 > 9) && ($1 < 100)) print substr($0,11,12), "      ", sub
str($0,14,35), "        ", substr($0,36);
          else if (($1 > 99) && ($1 < 1000)) print substr($0,10,12), "     ", su
bstr($0,14,35), "        ", substr($0,36);
          else print substr($0,9,12), "    ", substr($0,14,35), "        ", subs
tr($0,36);
         }' workunits-status.txt > workunits.txt
done

What it is not doing is giving me the spaces as specified in the print command and it is throwing out duplicate information on the same line. See example below.

Code:
WorkUnit:    Process-Name:        WF-Status:
4 pftestAssert02b       3         pftestAssert02b       3          3
14 pftestCus        pftestCustom01b       3          3
28 pftestAss        pftestAssert02b       3          3
38 pftestCus        pftestCustom01b       3          3
50 pftestAss        pftestAssert02b       3          3
60 pftestCus        pftestCustom01b       3          3
72 pftestMer        pftestMerc01          3          3

Please help. This has been day 2 and a trying experience. I have learned a lot from it already. Your help would be greatly appreciated.
# 9  
Old 11-12-2008
Quote:
Originally Posted by synergy_texas
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

...
Code:
awk 'BEGIN{printf "%-22s %-26s %s\n","WorkUnits:","Process-Name:","Status:"}{printf "%-22s %-26s %s\n",$1,$2,3}' file

# 10  
Old 11-13-2008
Hello guys!
I learned a lot from this thread, most of all that exec not always did what I was used to. Actually I've used that method in cmd (<file to set stdin for consecutive commands) and it is always good to see parallels.

Anyway, after Danmeros solution I just had to figure out a way to do it in more familiar grounds, awk and sed not being part of it and how OP started out with a while loop. I fiddled with different loops and smaller programs like tr, cut, column, etc and even thought about printf. But Danmero showed the way.

I wanted to do it in "pure" shell and given that printf usually is a built in, it should be faster that calling an external program... I guess it's a question of nanoseconds! Smilie


Quote:
(echo WorkUnits: Process-Name: Status:;cat src)|while read x y z;do printf "%-22s %-26s %s\n" $x $y $z;done
Well, for what it's worth, it's yet another way to do it. printf was the key.

/Lakris

PS And I hope it's not a totally worthless use use of cat...
# 11  
Old 11-13-2008
Quote:
Originally Posted by Lakris
..
I wanted to do it in "pure" shell and given that printf usually is a built in, it should be faster that calling an external program...
...
PS And I hope it's not a totally worthless use use of cat...
cat is external program and is useless Smilie
Code:
printf "%-22s %-26s %s\n" WorkUnits: Process-Name: Status:
while read x y z;do printf "%-22s %-26s %s\n" $x $y $z;done < file

# 12  
Old 11-13-2008
Hehe, right, I did EXACTLY that, hence my statement about external programs, but before I posted it I decided to try another one without TWO printf statements... I guess the cat bit the hand...

Yet another way!

Smilie

/Lakris
# 13  
Old 11-13-2008
I agree with Lakris, I have learned a lot from this post. It amazes me how you guys come up with 1 liners that sometimes take me up to 15 lines. You guys are awesome. I appreciate your help. I have already turned one of my coworkers on to this forum. You guys have been a great help and I will continue to research and use this forum for future issues. Hopefully, I will get better at scripting. Smilie
# 14  
Old 11-13-2008
Been there, done that, and lo and behold;
I went to The UNIX and Linux Forums - the Top UNIX & Linux Q&A on the Web

Now I learn a lot from the questions, and other peoples answers, giving me new views on problems that I didn't know I could face but sure as rain will (and has) hit me one day. It is a continuous learning experience.

Good luck

/Lakris
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