Apply command to all files in folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Apply command to all files in folder
# 8  
Old 10-06-2017
Hi.

This can be thought of as an ordering problem. Usually we think of situations like this in terms of character sorts.

However, we could also specify a set of strings to be used as the ordering sequence. In this case:
Code:
To:
Date:
Subject:

So we place these strings into a sort-order file, and sort the data files. Program msort can handle these kinds of tasks. Here is an example with 2 simple data files:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate sort, ordering by character strings, msort.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dixf

pl " Input sort-order file sort-order:"
head sort-order

pl " Input files data*:"
head data*

pl " Results:"
for j in data*
do
  msort -q -j -l -n 1,1 -s sort-order $j
  pe
done

pl " Some detail about msort:"
dixf msort

exit 0

producing:
Code:
$ ./s1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
dixf (local) 1.54

-----
 Input sort-order file sort-order:
To:
Date:
Subject:

-----
 Input files data*:
==> data1 <==
Subject:        Email test 1
From:   drl
To:     drl
Date:   Today 05:36
Hi ... cheers, drl

==> data2 <==
To:     drl
Subject:        Email test 1
Hi ... cheers, drl1
From:   drl
Date:   Today 06:36

-----
 Results:
To:     drl
Date:   Today 05:36
Subject:        Email test 1
From:   drl
Hi ... cheers, drl

To:     drl
Date:   Today 06:36
Subject:        Email test 1
From:   drl
Hi ... cheers, drl1


-----
 Some detail about msort:
msort   sort records in complex ways (man)
Path    : /usr/bin/msort
Version : 8.53
Type    : ELF64-bitLSBexecutable,x86-64,version1(SYSV ...)
Help    : probably available with -h,--help
Repo    : Debian 8.9 (jessie) 
Home    : http://www.billposer.org/Software/msort.html (pm)
Home    : http://billposer.org/Software/msort.html (doc)

The msort program is slower than the usual system sorting code, sort, but has a number of additional features that make its use very attractive.

As this demonstration illustrates, a single call to msort handles this task. The creation (and maintenance) of the sort-order file can be done easily with a text editor.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop awk command on files in a folder

Hi, I'd like to loop an action over all files with given extension within a folder. The "main" action is: awk -F "\t" 'BEGIN{OFS="\t"}{if ($10=="S") print$0; }' input.txt > output.txt The input.txt should be every file in the folder with *.subVCF extension; and the output should be a file... (3 Replies)
Discussion started by: dovah
3 Replies

2. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

3. Shell Programming and Scripting

find command to move the files to other folder - ksh 88

Hi , I've learnt that the following command will remove the files from the given folder for given no.of days find /home/etc -type f -atime -10 -exec rm -f {} \; But how can I change the above command that will move the files to another specified directory instead of removing the... (1 Reply)
Discussion started by: smile689
1 Replies

4. UNIX for Dummies Questions & Answers

Select all files in a folder based on creation date (ls command)

Hi All, <Re-posting in Correct group> I'm trying to select all the files in a folder that starts with a particular name format and are created in a gven date range using 'ls' command...but i'm not successful.... Example : I'm trying to see all the text files in a folder who names start... (6 Replies)
Discussion started by: Satya C1
6 Replies

5. Shell Programming and Scripting

reading information from a table and apply a command on multiple files

Hey gyuz, I wanna calculate the number of mapped reads of a bam file in a region of interest. I used this code to do so : samtools view input.bam chrname:region1 > region1.txt This will store all the reads from given bam file within the region of interest in region1.txt Now I have... (5 Replies)
Discussion started by: @man
5 Replies

6. UNIX for Dummies Questions & Answers

how to create a command that would print files only from 1 folder

Hi there how to create a command in csh that would print files only from 1 folder but as an argument takes home directory for e.g. in my home diecrtory I have 3 folders unix, windows,mac and alot of files. and I running the program as ./op ~ this should return me files from unix folder without... (1 Reply)
Discussion started by: FUTURE_EINSTEIN
1 Replies

7. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

8. UNIX for Advanced & Expert Users

UNIX: Command to compress folder and all files into a tar

I am trying to grab a folder and all the folders and files underneath it and send it from one computer to another. I basically want to compress the whole folder into a tar, tgz, or zip file so that it can be sent as one file. is there a command to compress a folder and all its contents into a tar... (7 Replies)
Discussion started by: kane4355
7 Replies

9. Shell Programming and Scripting

using mv command for moving multiple files in a folder

Hi, I have a requirement where I need to move Bunch of folders containing multiple files to another archive location. i want to use mv command .I am thinking when we use mv command to move directory does it create directory 1st and then move all the files ? e.g source... (4 Replies)
Discussion started by: rkmbcbs
4 Replies

10. UNIX for Dummies Questions & Answers

unix command to cound the number of files in a folder

Hi All Can some one help me out. Please tell the unix command to cound the number of files in a folder. Ungent please# Thanks manas (6 Replies)
Discussion started by: manas6
6 Replies
Login or Register to Ask a Question