LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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


Closed Thread
  Search this Thread
Old 01-23-2018, 04:48 PM   #1
Swathi Prasad
LQ Newbie
 
Registered: Jan 2018
Posts: 17

Rep: Reputation: Disabled
How this command works


Hi

I am bit new to Linux.

Can anybody explain How this command works

Code:
$ awk '/./{line=$0} END{print line}' file1
I know the above command prints the last non empty line.

for ex:file1 has text as shown below.


Code:
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
Total number of rows: 5
last 2 lines in the file are empty lines.

and the above comand prints

Total number of rows: 5

Can anybody please decode the command and explain me.

Thanks
 
Old 01-23-2018, 05:01 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by Swathi Prasad View Post
Can anybody explain How this command works

Code:
$ awk '/./{line=$0} END{print line}' file1
Putting the two code blocks on separate lines
Code:
/./ { line=$0 }
END { print line }
The first block matches any line that contains any character. The action block stores that entire line in the "line" variable, replacing whatever was there before.

The second block executes when end-of-file is reached on the input. The action block prints whatever is stored in the "line" variable.
 
1 members found this post helpful.
Old 01-23-2018, 05:02 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
The main loop (runs once per input line):
the regular expression . is true if there is any character met, then the following { block } is executed: the input line $0 is saved into the variable line.
At the END of the input file, the variable line is printed.
 
1 members found this post helpful.
Old 01-23-2018, 05:34 PM   #4
Swathi Prasad
LQ Newbie
 
Registered: Jan 2018
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you for your elegant explanations!!

I want to delete the same line(Total number of rows: 5) and copy the remaining lines to file2.file1 content should not change at all.
file1 should be as it is.

file2 should look like

Code:
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
To achieve above ,does my below code makes sense

Code:
$ awk '/./{line=$0} END{delete}' file1 >file2
If not how to achieve this?

Thanks
 
Old 01-23-2018, 06:01 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
grep -v '^Total' file1 > file2
 
Old 01-23-2018, 06:09 PM   #6
Swathi Prasad
LQ Newbie
 
Registered: Jan 2018
Posts: 17

Original Poster
Rep: Reputation: Disabled
I don't want to hard code any thing here,want to delete the last non empty line and copy the remaining text to the new file
 
Old 01-23-2018, 06:45 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Input file is no consistant? No way to find a pattern to keep or a pattern to leave?
There can be blank lines in the middle too?
 
Old 01-23-2018, 09:17 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
An answer was given in your other thread. This thread should be locked as a duplicate. (reported)
 
1 members found this post helpful.
Old 01-24-2018, 07:36 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



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
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
Restarting service using command works fine but using the same command in shell script fails to restart UserLinux123 Linux - Newbie 4 04-26-2016 09:47 AM
Command works when pasted at command line but not as bash script neild Programming 7 09-23-2012 07:30 AM
bash: command works at command line, but not via an alias porphyry5 Programming 3 03-17-2012 02:48 PM
busybox command works, same command within script says permission denied jeroenv Linux - Newbie 2 01-17-2009 02:58 AM

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

All times are GMT -5. The time now is 11:43 AM.

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
Open Source Consulting | Domain Registration