[Solved] awk manipulation of sequentially named files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] awk manipulation of sequentially named files
# 1  
Old 06-04-2013
[Solved] awk manipulation of sequentially named files

Hello, I am a very novice user of awk, I have a set of files named file001, file002, file003, file004, etc., each contains four fields (columns of data) separated each by a uneven number of spaces. I want to substitute those spaces by a TAB, so I am using this line of awk script:
Code:
awk -v OFS="\t" '$1=$1' file00X > filex00X

I've tried several ways of looping but all of them were unsuccessful, and although this is still way faster than introducing the TAB's by hand it still takes a lot of time to go through all of them.
Thanks,
# 2  
Old 06-04-2013
Quote:
Originally Posted by jaldo0805
Hello, I am a very novice user of awk, I have a set of files named file001, file002, file003, file004, etc., each contains four fields (columns of data) separated each by a uneven number of spaces. I want to substitute those spaces by a TAB, so I am using this line of awk script:
Code:
awk -v OFS="\t" '$1=$1' file00X > filex00X

I've tried several ways of looping but all of them were unsuccessful, and although this is still way faster than introducing the TAB's by hand it still takes a lot of time to go through all of them.
Thanks,
Close. Try:
Code:
for i in file???
do      awk -v OFS="\t" '$1=$1' "$i" > tmp$$
        cp tmp$$ "$i"
done
rm -f tmp$$

Almost any command that reads and writes a file using the command line syntax:
Code:
command option file > file
    or
command option < file > file

will have file truncated to size 0 by the shell before command starts running.

The cp in the script could be changed to mv (which would be more efficient) iff all of the files you want to update only have one link. (If you know that your input files all only have one link and you change the cp to mv, you can also get rid of the rm at the end of the script.)
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 06-04-2013
Thanks a lot, problem solved Smilie
# 4  
Old 06-04-2013
Another solution
Code:
awk '{$1=$1;print > FILENAME"_2"}' OFS="\t" file*

This creates new file like file001_2 file002_2 etc.
This User Gave Thanks to Jotne For This Post:
# 5  
Old 06-04-2013
Quote:
Originally Posted by Jotne
Another solution
Code:
awk '{$1=$1;print > FILENAME"_2"}' OFS="\t" file*

This creates new file like file001_2 file002_2 etc.
Note that this form will use up a file descriptor for every input file processed. Many awk implementations will run out of file descriptors if this script is given more than about 18 files.

If you're going to use this form (to keep your original files intact) when there is a large number of input files, you'll probably need to close the output files when you're done with them using something like:
Code:
awk '
FNR == 1 {
        if(of) close(of)
        of=FILENAME"_2"
}
{       $1=$1
        print > of
}' OFS="\t" file*

These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

Sequentially rename multiple files

Hello team, We wish to develop a script as follows : 1. Rename multiple files in the following way: example Original file names : test.txt and dummy.txt New file names : test.$(date +"%F").AAAAA<serialno_1>.BBBBBB.p and dummy.$(date +"%F").AAAAA<serialno_2>.BBBBBB.p 2. The... (3 Replies)
Discussion started by: H squared
3 Replies

3. Shell Programming and Scripting

Match multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

4. Shell Programming and Scripting

[Solved] Data manipulation

Hallo Team, I need your help. I have a file that has two colums. See sample below: 105550 0.28 105550 0.24 125550 0.28 125550 0.24 215650 0.28 215650 0.24 315550 0.28 315550 0.24 335550 0.28 335550 0.24 40555 0.21 40555 0.17 415550 0.21 415550 0.17 43555 0.21 43555 0.17 (5 Replies)
Discussion started by: kekanap
5 Replies

5. Shell Programming and Scripting

[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this: Generated geometry (...some special descriptor) 1 0.56784 1.45783 -0.87965 8 1.29873 -0.8767 1.098789 ... ... ... ... Generated geometry (....come special descriptor) ... .... ... ... ... ... ... ... and... (4 Replies)
Discussion started by: jaldo0805
4 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Text manipulation help

Hello Unix.com How can I sort from a large email list only the emails that finish with .ca domain? cat <list> | grep "\.ca\b" >> <new list> isnt working perfectly. Any tips? Best regards, Galford D. Weller (2 Replies)
Discussion started by: galford
2 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Column manipulation

Hi Everyone, I was wondering if someone could help me to transform my data into a format I need. Here is an example of what my data looks like E F G H A 1 2 3 4 B 5 6 7 8 C 9 1 2 3 D 4 5 6 7 and this is what I would need it to look like: AE 1 BE 5 CE 9 DE 4 AF 2 BF 6 CF 1 (6 Replies)
Discussion started by: zajtat
6 Replies

8. Shell Programming and Scripting

[SOLVED] Handling multiple files using awk

Hi, I am trying to process 2 files simultaneously using awk satisfying following condition, Both files contain 3 columns. It should take entry from column 1 from first file, look for that entry in file 2 and if found, add column 2 and column 3 from both files and output to third file. For e.g.... (4 Replies)
Discussion started by: muazfarooqaslam
4 Replies

9. Shell Programming and Scripting

Finding missing files that are named sequentially with Perl?

Hello I am new to Perl, in fact I am on chapter one of the book. :) However I am in need of a Perl Script faster than I can finish the book. Perhaps someone can help me with my immediate need while I read my book. I have a directory with hundreds of files that are all named like... (4 Replies)
Discussion started by: newftronics
4 Replies

10. UNIX for Advanced & Expert Users

FTP Files Sequentially

Hi Gurus, i have to transfer files one by one from ftp server to target server all files which is to be transferred lies in one ftp folder i have to move those files sequentially from ftp to target and must verify files for successful transmission . then i have to delete corresponding... (1 Reply)
Discussion started by: harim
1 Replies
Login or Register to Ask a Question