awk to perform splitting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to perform splitting
# 1  
Old 07-30-2015
awk to perform splitting

Dear all,
In my working directory I have two files as:
Code:
$ ls -ltr
-rw-r--r-- 1 emily af-cms   7 30. Jul 09:56 L1File1_000_981.root
-rw-r--r-- 1 emily af-cms  10 30. Jul 09:56 L1File2_000_980.root

I want to have the OutputFile.txt with the following content:
Code:
L1File1_000_981.root L1File2_000_980.root

I am in process of writing the script using the awk to perform the splitting. The script can be found here [1], which you can see is not in the final form.

Any suggestions, why the 'OutputFile.txt' shows only the one entry?

Thanks in advance,
emily

[1]
Code:
#!/bin/bash                                                                                                                                                                                                    
OUTPUT=OutPutFile

for File in *root
do
    echo $File
    NewFile=$(echo $File | awk '{split($0, a, "root"); print a[1]}')
    echo $NewFile > $OUTPUT
done

# 2  
Old 07-30-2015
Yes, its because you overwrite the output file with > instead of appending to it with >> redirection operator.

But - why do you chip the "extension" off the file name when it is part of the desired output?

Try
Code:
echo *.root > $OUTPUT

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-30-2015
Thanks, yes '>>' works.
As I wrote, I was in the process of writing the script, which needed modifications..Smilie I used the 'awk' as I wanted the output to be in continuous line instead of falling on the other line. I thought, AWK would give me this flexibility, which I am still trying to figure out 'HOW'.

Any input on how the AWK can be used to have the output in single line?

Greetings,
Emily,

---------- Post updated at 03:49 AM ---------- Previous update was at 03:42 AM ----------

I achieved it...Smilie
Not the best script, but it works..
Code:
#!/bin/bash                                                                                                                                                                                                    
OUTPUT=OutPutFile

rm $OUTPUT
for File in *root
do
    if [[ ! -e "dest_path/$File" ]]; then
        NewFile=$(echo $File | awk '{split($0, array, "root"); print array[1]"root"}')
        echo $NewFile | awk '{print}' ORS=' ' >> $OUTPUT
    fi
done

# 4  
Old 07-30-2015
Use printf with an adequate format string in lieu of print (which has an implied <new line>).
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-30-2015
Question

Why use the AWK, which, BTW, is a terrific language? It does have some overhead/startup costs. I just pegged myself as a 300Mhz P3 user, didn't I? Smilie
Code:
$ ls -l *.root
-rw-rw-r-- 1 mhofer mhofer 0 Jul 30 07:42 L1File1_000_981.root
-rw-rw-r-- 1 mhofer mhofer 0 Jul 30 07:42 L1File2_000_980.root

$ ls *.root | tr "\n" ' ' > OutPutFile

$ cat OutPutFile
L1File1_000_981.root L1File2_000_980.root

I'm pretty sure something could be done with sed on the output of ls *.root as well,

[later] Of course

Code:
$ ls -x *.root > OutPutFile

$ cat OutPutFile 
L1File1_000_981.root  L1File2_000_980.root

works as well but note the extra separating space. SmilieSmilie

Last edited by featheredfrog; 07-30-2015 at 09:13 AM.. Reason: more obnx
This User Gave Thanks to featheredfrog For This Post:
# 6  
Old 07-30-2015
Great, thanks..Smilie Smilie
# 7  
Old 07-31-2015
Instead of appending each output with >> (which denotes open,seek,write,close each time), you can redirect the entire loop
Code:
for i in a b c
 do
 echo "$i"
done > outfile

The shell opens the file when the loop starts and closes it when the loop ends.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script concatenate two column and perform mutiplication

Need your help in solving this puzzle. Any kind of help will be appreciated and link for any documents to read and learn and to deal with such scenarios would be helpful Concatenate column1 and column2 of file 1. Then check for the concatenated value in Column1 of File2. If found extract the... (14 Replies)
Discussion started by: as7951
14 Replies

2. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

3. Shell Programming and Scripting

Why awk perform differently when using variable?

Hi Gurus, I hit a hard block in my script. when using awk command with variable, I got different result. Please see below: my test file as below: $ cat demofile.txt filename-yyyy-abcd filename-xxx-week-pass filename-xxx-week-run for testing purpose, I put 3 awk command in one script.... (7 Replies)
Discussion started by: ken6503
7 Replies

4. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

5. Shell Programming and Scripting

How to perform averaging of values for particular timestamp using awk or anythoing else??

I have a file of the form. 16:00:26,83.33 16:05:26,83.33 16:10:26,83.33 16:15:26,83.33 16:20:26,90.26 16:25:26,83.33 16:30:26,83.33 17:00:26,83.33 17:05:26,83.33 17:10:26,83.33 17:15:26,83.33 17:20:26,90.26 17:25:26,83.33 17:30:26,83.33 For the timestamp 16:00:00 to 16:55:00, I need to... (5 Replies)
Discussion started by: Saidul
5 Replies

6. Shell Programming and Scripting

How To Perform Mathematical Operation Within If in awk?

Hi All, I am using an awk script as below: awk -F'|' 'BEGIN{OFS="|";} { if ($1==$3 && $3==$7 && $7==$13 && $2==$6 && $6==$11 && $15-$14+1==$11) print $0"|""TRUE"; else print $0"|""FALSE"; }' tempfile.txt In above script, all conditions are being checked except the one which is... (4 Replies)
Discussion started by: angshuman
4 Replies

7. Shell Programming and Scripting

perform echo and awk inside a string

hi, just wanted to make a shortcut of this one a="a b c" b=`echo $a | awk '{print $2}'` echo "the middle is $b" why can't i do this: a="a b c" echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution :wall: thanks (6 Replies)
Discussion started by: h0ujun
6 Replies

8. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies

9. UNIX for Dummies Questions & Answers

Record splitting with AWK

Hi all ! I need your help as quick as possible. My input file like this: bạc těnh ( 薄情) 1 . 薄情な.2. 夫婦或いは男女の不貞を指す。 bách (百,迫)1.100ドソ. tr a m b a c ともいう. 2.柏(カヽしわ)・ 3.圧迫する.4.差し迫った, My propose is take the value in the firt bracket. I used the command like : ...if (index(... (6 Replies)
Discussion started by: maixu134
6 Replies

10. Shell Programming and Scripting

Splitting a string with awk

Hi all, I want to split a string in awk and treat each component seperatley. I know i can use: split ("hi all", a, " ") to put each delimited component into array a. However when i want to do this with just a string of chars it does not work split ("hi", a, ""); print a; prints... (6 Replies)
Discussion started by: pxy2d1
6 Replies
Login or Register to Ask a Question