Sponsored Content
Top Forums Shell Programming and Scripting Apply command to all files in folder Post 303004670 by Don Cragun on Friday 6th of October 2017 03:04:56 AM
Old 10-06-2017
Instead of invoking grep twice and sed once for each file to be processed, one might also consider just using ed:
Code:
for fname in *.txt
do	if [ "$fname" != "${fname%_1.txt}" ]
	then	printf '%s has already been processed.\n' "$fname"
		continue
	fi
	newfname=${fname%.txt}_1.txt
	printf 'Creating "%s" from "%s"...\n' "$newfname" "$fname"
	ed -s "$fname" <<-EOF && echo rm "$fname"
		H
		g/^Subject: /.m0
		g//.m0\\
		    s///
		g/^Date: /.m0
		g//.m0\\
		    s///
		g/^To: /.m0
		g//.m0\\
		    s///
		w $newfname
		q
	EOF
done

Note that this will skip files with names ending with _1.txt and that it will put all To: lines first in the output, followed by all Date: lines, followed by all Subject: lines (each of the above with the keywords removed), followed by the remaining lines in the original file. If you know beforehand that there will never be more than one of the header lines of each of the three header line types, this code could be further simplified.

Note that the above code uses tabs (not spaces) to indent lines. If you convert those tabs to spaces, the shell won't find the end of the here-document.

Note also that this code uses:
Code:
echo rm "$fname"

which just prints the command to remove the old files. If, after testing, you find that the code does what you want; remove the echo to actually remove the old files.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GETLASTLOGX(3)						   BSD Library Functions Manual 					    GETLASTLOGX(3)

NAME
getlastlogx, getutmp, getutmpx, updlastlogx, updwtmpx, utmpxname -- user accounting database functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <utmpx.h> struct lastlogx * getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); void getutmp(const struct utmpx *ux, struct utmp *u); void getutmpx(const struct utmp *u, struct utmpx *ux); int updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); int updwtmpx(const char *file, const struct utmpx *utx); int utmpxname(const char *fname); DESCRIPTION
The getlastlogx() function looks up the entry for the user with user id uid in the lastlogx(5) file given by fname and returns it in ll. If the provided ll is NULL, the necessary space will be allocated by getlastlogx() and should be free()d by the caller. The getutmp() function fills out the entries in the struct utmp u with the data provided in the struct utmpx ux. getutmpx() does the oppo- site, filling out the entries in the struct utmpx ux with the data provided in the struct utmp u, and initializing all the unknown fields to 0. The sole exception is the ut_type field, which will be initialized to USER_PROCESS. The updlastlogx() function tries to update the information for the user with the user id uid in the lastlogx(5) file given by fname with the data supplied in ll. A struct lastlogx is defined like this: struct lastlogx { struct timeval ll_tv; /* time entry was created */ char ll_line[_UTX_LINESIZE]; /* tty name */ char ll_host[_UTX_HOSTSIZE]; /* host name */ struct sockaddr_storage ll_ss; /* address where entry was made from */ }; All the fields should be filled out by the caller. The updwtmpx() function updates the wtmpx(5) file file with the utmpx(5) entry utx. The utmpxname() function sets the default utmpx(5) database file name to fname. RETURN VALUES
getlastlogx() returns the found entry on success, or NULL if it could not open the database, could not find an entry matching uid in there, or could not allocate the necessary space (in case ll was NULL). utmpxname() returns 1 on success, or 0 if the supplied file name was too long or did not end with 'x'. updlastlogx() and updwtmpx() return 0 on success, or -1 in case the database or file respectively could not be opened or the data not written into it. SEE ALSO
endutxent(3), loginx(3), utmpx(5) HISTORY
The functions getutmp(), getutmpx(), updwtmpx(), and utmpxname() first appeared in Solaris. getlastlogx and updlastlogx first appeared in NetBSD 2.0. BSD
August 26, 2003 BSD
All times are GMT -4. The time now is 01:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy