How can I make my script simple?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I make my script simple?
# 1  
Old 01-31-2013
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

# 2  
Old 01-31-2013
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

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

# 4  
Old 01-31-2013
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

# 5  
Old 01-31-2013
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

# 6  
Old 01-31-2013
Quote:
Originally Posted by nex_asp
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.
# 7  
Old 01-31-2013
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make it simple?

if ];then echo "ifconfig has output,and the output is " ifconfig -a fi this would run twice for "ifconfig -a" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

2. Linux

How to execute a simple select script using a shell script?

Hi team, I have two select statements and need to run them using SYSDBA user select * from temp_temp_seg_usage; select segment_name, tablespace_name, bytes/ (1024*1024) UsedMb from dba_segments where segment_name='TEMP_TEMP_SEG_USAGE'; Need to run this using a shell script say named... (1 Reply)
Discussion started by: pamsy78
1 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Solaris

Trying to do a simple Make!

I am trying to do a make on below and have a question: http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz I am running Solaris 10 on x86. I have untarred the files above. I have gcc and gmake in /usr/sfw/bin BUT when I do >gmake in the directory with above files, it does not... (4 Replies)
Discussion started by: steve701
4 Replies

5. Shell Programming and Scripting

Script to make simple recurring ascii file edit

Hi, I have an ascii file with recurring lines (the file is 36mb so lots of lines) which look like this: -2.5 -66.324-68.138 935.2 1.953 -0.664 272.617 73.684 -2.428 269.998 0.000 Every 14 lines there is a blank line. I would like to, for each non-blank line,... (2 Replies)
Discussion started by: blueade7
2 Replies

6. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions: 1. How to write a script in HP-UX 11. 2. How to schedule a script. 3. How to "call" scripts from the original script. 4. How to su to another user from within a script. This is the basics of what the... (15 Replies)
Discussion started by: instant000
15 Replies

7. Shell Programming and Scripting

Help to make the script simple

Hi All, I have a script which throws the output if condition matches. I run the cmd : # ldf Filesystem kbytes used avail capacity Mounted on /dev/dsk/c1t0d0s0 1984564 1375019 550009 72% / /dev/dsk/c1t0d0s3 5040814 2628410 2361996 53% /usr... (4 Replies)
Discussion started by: naw_deepak
4 Replies

8. Shell Programming and Scripting

make it simple!!!

i am having following DML file i want to extract only highlighted area using sed or awk oneliner.. i wrote the following sed command for this .. it works fine but its too complex i guess.. can any one help me out to make it simpler.. thanks in advance.. vidya.. (2 Replies)
Discussion started by: vidyadhar85
2 Replies

9. UNIX for Dummies Questions & Answers

Simple make file questions....i think, thnx

Hello, I'm a noob when comes to make files.... My intentions for the use of my make file are not that of a usual compilation, etc. It is simply to copy some files from a RCS controlled area to a public area which has read rights only for a web page. My dilemma comes in the form of sub... (0 Replies)
Discussion started by: Roxydogg28
0 Replies

10. Programming

make script

I need to write a make script to install a C module in a UNIX environment.It should install the sources, build the libraries and install them and also install the info pages on the system. Can this script be general enough to also install on windows, windows dll, windows help file's etc. Any... (3 Replies)
Discussion started by: cherio
3 Replies
Login or Register to Ask a Question