Difference in awk output and while


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference in awk output and while
# 1  
Old 12-03-2017
Difference in awk output and while

so, im going over one of my scripts and trying to optimize it.

i have a code like this:

Code:
cksum sjreas.py | awk '{prinnt $1$2}'

This does what I need. However, i dont want to call the external command awk. so im doing this:

Code:
cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf "${v1}${v2}"; done

The problem is, the while loop seems to have a problem with the spaces. When i tell it above to grab only the first two values from the output of cksum, it also grabs the space that's right after v2.

Is there a way to make the while loop operate exactly like awk and only grab the field you tell it to grab?

btw, the output of cksum, without while or awk being used is this:

Code:
3558287950 577 sjreas.py

# 2  
Old 12-03-2017
Since you call the external cksum command, I do not see the value of losing the call to awk ... unless you have this code execute hundreds of times in a loop we did not see.

Code:
while read v1 v2 v3 
do
    echo "$v1$v2"
done < cksum sjreas.py
# or
 declare -a arr=(`sjreas.py`)   # use backticks it is clearer IMO
# you now have an array
echo "${arr[0]}${arr[1]}"


Last edited by jim mcnamara; 12-03-2017 at 04:20 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-03-2017
If you are using bash, ksh93 or zsh, you could do something like this
Code:
read v1 v2 dummy < <(cksum sjreas.py)

---
The reason you get a trailing space in your variable may also be because of the value that you used for IFS.
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-04-2017
Quote:
Originally Posted by SkySmart
. . .
Code:
cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf "${v1}${v2}"; done

The problem is, the while loop seems to have a problem with the spaces.
. . .
You've got two things deserving comments in above:
- the OFS variable has a meaning in awk (only / mostly). for shell, use IFS.
- as your v2 variable holds the remainder of the input line, i.e. 577 sjreas.py, I'd be surprised if the printf command only outputted a space, and not the file name, because, due to the quoting, everything would be the first parameter to printf and thus its "format string".
# 5  
Old 12-04-2017
You can also remove the filename from the output of cksum:
Code:
$ cksum file
812788392 1423 file
$ cksum < file
812788392 1423

Obviously you cannot do this if getting the checksum of multiple files.

Andrew
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Difference output of files

Need help on below req Compare two files and send difference of file to other file File2 is static which never changes ex: File1 A.txt B.ttx C.txt E.txt File2 A.txt (6 Replies)
Discussion started by: satish1222
6 Replies

3. Shell Programming and Scripting

Gawk output difference

Why the below option2 doesn't fetch similar output as option1 ? I am on linux. $cat test 2013-01-01-00.25.43.643845 Option1: cat test | gawk -F"-" ' {print $2 " " $3 " " $1}' 01 01 2013 Option2: cat test | gawk '{FS="-"} {print $2 " " $3 " " $1}' 2013-01-01-00.25.43.643845 (5 Replies)
Discussion started by: Shivdatta
5 Replies

4. Shell Programming and Scripting

Need script to output difference in folders

Hey everyone, I have two folders (folder1, folder2). Folder2 is a compiled version of a bunch of other folders including folder1. I want to compare folder1 to folder2 to make sure that folder2 contains all of the contents of folder1. If it does not, I would like the script to output... (5 Replies)
Discussion started by: chango77747
5 Replies

5. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

6. UNIX for Dummies Questions & Answers

Check for difference in output of 2 commands?

Hello! I'm just learning the shell, and I would really like to know how to do this: Given these 2 commands: ls -l ls -le How can I, with a one-liner, ask the shell to show me visually in the shell, what the difference is between the output of the two commands? They look the same to me... (6 Replies)
Discussion started by: turbofayce
6 Replies

7. Solaris

Difference in date output

HiCan anyone tell me why I am getting a difference in the date format on 2 different Solaris servers?On one I get: -Monday, 9 November 2009 09:02:45 GMTand the other: -Monday November 9 09:03:05 GMT 2009Both servers are running OS Version M-11/16/88iCan anyone tell me why one uses a "," and the... (5 Replies)
Discussion started by: steadyonabix
5 Replies

8. HP-UX

Difference in netstat -a and -an output.

Hi, Does anyone know why I get a different output when using "netstat -a" or "netstat -an" ?? # netstat -a | grep ts15r135 tcp 0 0 nbsol152.62736 ts15r135.23211 ESTABLISHED # netstat -an | grep 172.23.160.78 tcp 0 0 135.246.39.152.51954 ... (4 Replies)
Discussion started by: ejdv
4 Replies

9. UNIX for Dummies Questions & Answers

output difference in two files

Hi, Please help me, I have two files. I need to output the difference of contents of each file in another file. For example, I need to know the content of the file1 that does not exist on file2 and vice versa. Please take note that the size of the files are large. How can I do it using unix... (4 Replies)
Discussion started by: ayhanne
4 Replies

10. UNIX for Dummies Questions & Answers

How to output the difference of two files?

Hi, I had two data file (File1, File2), each one just have one column, but two file were very big. File2 is smaller, all its data included in File1. I want to ouput the result which don't have any data in File2. Could any one give me a help on how to do that? Thanks in advance! Yun ... (4 Replies)
Discussion started by: yxiao
4 Replies
Login or Register to Ask a Question