How can I make my script simple? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How can I make my script simple?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2013
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts
How can I make my script simple?

Hi .. I am trying to print first row few columns and last row few column...

I am doing like this... I want to do using single awk



Code:
for file in *.xyz; do
dt_end=$(awk 'END{print $2 "\t" $3 "\t" $4}' FS="," $file)
dt_start=$(awk 'FNR == 1{print $1 " \t"$2 }' FS="," $file )
echo $dt_start $dt_end
done

Sponsored Links
    #2  
Old 01-31-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
Try sth like this..


Code:
 
awk 'FNR==1{printf $1 " \t"$2" "}
FNR==1 && NR>1 {print s} 
{s=$2"\t"$3"\t"$4}' FS="," *.xyz

Sponsored Links
    #3  
Old 01-31-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 April 2014, 9:30 AM EDT
Posts: 313
Thanks: 32
Thanked 81 Times in 80 Posts

Code:
awk -F, 'FNR==1{print $1,$2}END{print $2,$3,$4}' OFS=, *.xyz

    #4  
Old 01-31-2013
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts

Code:
suppose if I want print dt_end in 
awk 'FNR == 1{print $1 " \t"$2 } FS="," file

How to include variable dt_end in above

---------- Post updated at 05:53 AM ---------- Previous update was at 05:41 AM ----------

Out put not coming properly...

need like this


Code:
dt_start \t dt_end

Sponsored Links
    #5  
Old 01-31-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 April 2014, 3:26 PM EDT
Location: Aachen, Germany
Posts: 3,525
Thanks: 51
Thanked 820 Times in 783 Posts
Be careful to run this only on one single file at a time. If you run it on multiple files, there'd be many starts and ends that will be more difficult to separate. Try this:
Code:
$ tmp=$(awk -F, 'NR==1{print $1,$2}  END{print $2,$3,$4}'  OFS=,  ORS=" "  A.xyz)
$ dt_start=${tmp%% *}
$ dt_end=${tmp#* }
$ echo "$dt_start      $dt_end"
40454,31    51358,834,743

Sponsored Links
    #6  
Old 01-31-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 April 2014, 11:46 PM EDT
Location: San Jose, CA, USA
Posts: 3,468
Thanks: 141
Thanked 1,197 Times in 1,015 Posts
Quote:
Originally Posted by nex_asp View Post
Code:
suppose if I want print dt_end in 
awk 'FNR == 1{print $1 " \t"$2 } FS="," file

How to include variable dt_end in above

---------- Post updated at 05:53 AM ---------- Previous update was at 05:41 AM ----------

Out put not coming properly...

need like this


Code:
dt_start \t dt_end

In your 1st posting in this thread, you wanted the output to be

Code:
L1F1 \tL1F2 LLF2\tLLF3\tLLF4\n

for each input file where L1F1 is the contents of Line 1 Field 1, L1F2 is Line 1 Field 2, LLF2 is Last Line Field 2, LLF3 is Last Line Field 3, and LLF4 is Last Line Field 4.

Here you are saying the output you want is:

Code:
L1F1 \tL1F2 \t LLF2\tLLF3\tLLF4\n

for each input file.

If I'm reading the code supplied by others correctly, pamu's code will give you the information you asked for for every file except the last file given, but the 1st two fields of the 1st two files will be printed before the last fields of the 1st file and subsequent lines (until the last file) will have the 1st fields from file x and the last fields from file x-1. The fields from the last line of the last file will not be printed. In addition, if L1F1 or L1F2 in any file contains a % character or a \ character, the results might not be what you requested.

Subbeh's code will only print the 1st line data from the 1st file and the last line data from the last file.

And, RudiC's code will print:

Code:
L1F1,L1F2      LLF2,LLF3,LLF4\n

as long as there are no spaces in any of the fields being printed, but will only process one file at a time.

Note also that the standards do not define the values of $2, $3, or $4 in an END action (but I don't know of any implementation of awk that doesn't do what you want). The code pamu provided is the only one that handles this portably.

I believe the following script does what you requested (as modified above) portably:

Code:
awk -F, '
FNR == 1 && NR > 1 {    print s}
FNR == 1 {              printf("%s \t%s \t ", $1, $2)}
{                       s = $2 "\t" $3 "\t" $4}
END {                   print s}' *.xyz

as long as you /usr/xpg4/bin/awk or nawk rather than awk if you are running on a Solaris/SunOS system.
Sponsored Links
    #7  
Old 01-31-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 April 2014, 3:26 PM EDT
Location: Aachen, Germany
Posts: 3,525
Thanks: 51
Thanked 820 Times in 783 Posts
Ouch, I missed the separating <TAB>s! And, I concentrated to get those fields into the variables dt_start and dt_end, assuming the requestor wanted to process them further. FAILED by far!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Trying to do a simple Make! steve701 Solaris 4 09-07-2011 03:06 AM
Script to make simple recurring ascii file edit blueade7 Shell Programming and Scripting 2 08-22-2011 02:23 PM
Help to make the script simple naw_deepak Shell Programming and Scripting 4 04-14-2009 06:44 AM
make it simple!!! vidyadhar85 Shell Programming and Scripting 2 08-21-2008 02:20 AM
Simple make file questions....i think, thnx Roxydogg28 UNIX for Dummies Questions & Answers 0 02-01-2008 04:39 AM



All times are GMT -4. The time now is 11:46 PM.