LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-04-2015, 10:41 AM   #1
florence_renalier
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Rep: Reputation: Disabled
difference between "awk '{print $0}'" and "cat"


Hello,

I am working on windows in the Cygwin environment.

I have a textfile (output from a specific software working on windows) that I would like to copy exactly with the command "awk '{print $0}' filename".

When I open my original file with vi, it indicates at the bottom that there are 3102 characters.

When I open the file copied with the "awk '{print $0}' filename" command (which really looks like the 1st one), vi only indicates 3031 characters.

When I open a file copied with the "cat filename", vi indicates 3102 characters.

Why would the "awk '{print $0}' filename" and the "cat filename" commands be different ? Any idea on what specific character has disappeared with the awk command ?

As I said, looking at the 2 files, they do not look different. However, the file copied with awk can not be imported in the specific software anymore, whereas the file copied with cat can ...

Remark : I would like to use the awk command because in the end I would like not to copy the whole file, but only specific lines that are easier to select with awk (at least, I know how to do ...).

This would maybe be clearer if I could share the file but I do not know how to do ...

Thank you very much in advance for your answers,

Best regards,

Florence.
 
Old 12-04-2015, 10:46 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
As the file being dealt with initially is from Windows, it will have Windows line endings (\r\n), but after being run through awk I would guess these have all been changed to the linux line ending (\n).
cat on the other hand simply regurgitates the file as is (to the best of my knowledge)

Due to the above affect of awk, your Windows program now does not know where a line is ending. To fix you can simply tell awk to replace the ORS value with the Windows line ending.
 
Old 12-04-2015, 11:08 AM   #3
florence_renalier
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you very much for your fast answer.
Could you explicit, please, how to "tell awk to replace the ORS value with the Windows line ending".
Thanks a million,
Florence.
 
Old 12-04-2015, 12:01 PM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by florence_renalier View Post
Thank you very much for your fast answer.
Could you explicit, please, how to "tell awk to replace the ORS value with the Windows line ending".
Thanks a million,
Florence.
Like this:
Code:
awk '{ sub(/$/,"\r"); print }' < lnx_line_end.txt > win_line_end.txt
The infile (lnx_line_end.txt) has Unix line feeds (LF), the outfile (win_line_end.txt) that awk produces has Windows line feeds (CRLF).

Best regards,
HMW
 
Old 12-04-2015, 12:06 PM   #5
florence_renalier
LQ Newbie
 
Registered: Dec 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
great ! It works all fine ! Thanks a million to both of you !
Best regards,
Florence.
 
Old 12-04-2015, 12:51 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Actually i would have just done:
Code:
awk 'ORS="\r\n"' file > win_file
 
Old 12-04-2015, 01:07 PM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by grail View Post
Actually i would have just done:
Code:
awk 'ORS="\r\n"' file > win_file
^That's because you are better @ awk than I am!

Best regards,
HMW
 
Old 12-04-2015, 01:39 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
hehe ... I was not trying to upstage anyone Plus, I feel I have had a fair bit of practice.
 
Old 12-04-2015, 03:07 PM   #9
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by grail View Post
hehe ... I was not trying to upstage anyone
I didn't take it that way. You simply ARE way better with awk than I am. Credit where credit is due!

Best regards,
HMW
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 05:08 PM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 03:55 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 04:30 PM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 08:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration