Sponsored Content
Full Discussion: If based on filename?
Top Forums Shell Programming and Scripting If based on filename? Post 302115740 by anbu23 on Friday 27th of April 2007 01:13:45 AM
Old 04-27-2007
Add >> operator and keep the if else statement inside for loop
Code:
	for filename in *XG.txt; do
	 #create header record
	if test -f $filename
 	then
	 #if filename begins with AA, do this
	 awk '/^0/ && FILENAME ~ /^AA/ { print "AA"substr($0,10,6)"00"}' $filename > TEST_HEADER_A
	 
	 #if filename doesn't begin with AA but begins with XX, do this
	 awk '/^0/ && FILENAME ~ /^XX/ { print "XX"substr($0,10,6)"00"}' $filename >> TEST_HEADER_A
	
   #awk '/^0/ {if (substr($filename,1,2)=="AA") print "AA"substr($0,10,6)"00"}' $filename > TEST_HEADER_A
   #awk '/^0/ {if (substr($filename,1,2)=="XX") print "XX"substr($0,10,6)"00"}' $filename > TEST_HEADER_A

	 awk '/^9/ {print substr($0,5,5)}' $filename > TEST_HEADER_B
	 awk '/^9/ {print substr($0,14,10)}' $filename > TEST_HEADER_C
	 awk '/^9/ {print "00000""0000000000"}' $filename > TEST_HEADER_D
	 awk '/^0/ {print substr($0,2,6)}' $filename > TEST_HEADER_E

	 #produce transaction records
	 awk '/^0/ {print ""}' $filename > TEST_TRANS

	 nawk '/^1/ {
	 if(substr($0,23,3) == "000") {printf("%.5d%s\n",NR-1,substr($0,14,9)"999""  ""XXXX"substr($0,2,6)substr($0,32,7)"+""          NEW FILE""TEST      ""          ""N")} 
	 else {printf("%.5d%s\n",NR-1,substr($0,14,12)"  ""XXXX"substr($0,2,6)substr($0,32,7)"+""          NEW FILE""TEST      ""          ""N")} 
	 }' $filename >> TEST_TRANS

	 #cat sub headers together to create TEST_HEADER
	 cat TEST_HEADER_A TEST_HEADER_B TEST_HEADER_C TEST_HEADER_D TEST_HEADER_E | tr -d "\n" > TEST_HEADER

	 #cat header and trans files together to create TEST.IN
	 cat TEST_HEADER TEST_TRANS > TEST.IN

	 #move TEST.IN to $TEST_LOG
	 mv TEST.IN $TEST_LOG/'TEST.IN_'$filename

	 #clean up temp files
  	 rm TEST_HEADER
	 rm TEST_TRANS
	 rm TEST_HEADER_A
	 rm TEST_HEADER_B
	 rm TEST_HEADER_C
	 rm TEST_HEADER_D
	 rm TEST_HEADER_E
	else
		echo 'process bypassed - '$filename' file not available in the '$PWD' directory'
	fi
	done
	echo 'process complete'

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to move files into different folders based on filename

I need to move a bunch of files into folders that have the same name. I wanted to either do this with some filter command or some type of batch file that I could save that would already include all of the mv commands since I will have to do this process often. Whatever method you think is easier. ... (7 Replies)
Discussion started by: italia5
7 Replies

2. UNIX for Dummies Questions & Answers

mv files based on filename

I have a directory of about 30,000 image files. The file names are all yearmonthday.jpg however some of the files have yearmonthday-snapshot.jpg i would like to move all files that contain the phrase -snapshot to their own directory. Any assistance with the proper commands would be much... (1 Reply)
Discussion started by: jdblank
1 Replies

3. Shell Programming and Scripting

Print only Filename based on given String on file name

Hi, I am new to this unix world. Any ways, I would like to write a shell script that can print the file name. Ex : directory will have 5 files with different name.No matter what are contents are. Now I need to find the file which will have particular name (sub string ).Please do not... (5 Replies)
Discussion started by: akb2010
5 Replies

4. Shell Programming and Scripting

Move file based on filename

Hi All I need a script to manipulate files based on a filename: example filename: 66600_042706.pdf the script will create a directory 66000 only if this directory is not existing. If that directory is existing it will just move the file to 66000/666000_042706.pdf in addition, i want to... (4 Replies)
Discussion started by: aemestech
4 Replies

5. Shell Programming and Scripting

Move files based on date in filename

I know this gets covered quite a bit in the forum and I think there is enough there for me to figure out how to do what I am trying to do, I just don't think I would do it very efficiently so I am going to ask the question... I have database log files with date and time stamps in the file like ... (7 Replies)
Discussion started by: slatoms
7 Replies

6. Shell Programming and Scripting

Send email recipient based on filename

Hi All can someone help please create a bash script. Here's a scenario: 1. I have a directory where it's a destination for scanned documents. e.g. /dest/scan 2. The filename is in the form IDNumber_Category. e.g. 123456_notes.pdf 3. The first part of the script is to rename the... (1 Reply)
Discussion started by: aemestech
1 Replies

7. Shell Programming and Scripting

awk based on filename

i need to use awk to replace string in file. if suppose filename is abcd.dat i need to replace in file california with CA else if file name name is cdef.dat i need to replace in file california with CALI i need to use awk(must) any suggestion how can i do that file structure ... (3 Replies)
Discussion started by: er_zeeshan05
3 Replies

8. UNIX for Dummies Questions & Answers

Select max value based on filename

Hi, I would just like to know how to get the file with the max filename on a directory and get rid of all the others. For example, in directory A:/ i have the ff files: APPLE2001 APPLE2002 APPLE2003 GRAPE2004 what I want to get is the max in files whose filenames start with APPLE*,... (4 Replies)
Discussion started by: madden
4 Replies

9. UNIX for Dummies Questions & Answers

Finding filename based on filecontent

Hi, I have been trying , to find the filename based on some pattern present inside the file My command is as follows: filename=`grep -l 'Pattern' path/*.txt ` Its strange that it works some times, but doesn't print anything some times . But my if test -f $filename is passing all the... (2 Replies)
Discussion started by: Prashanth19
2 Replies

10. Shell Programming and Scripting

Sorting based on filename

Hello , I have to write a bash script. I will explain the logic based on a scenario. Scenario : Suppose I have few files in a Folder X : FILE_201508.list FILE_201510.list FILE_201507.list abc_201510.csv xyz_201508.csv abc_201507.csv def_201507.csv 1) Now ,... (3 Replies)
Discussion started by: smamrm
3 Replies
bytes(3pm)						 Perl Programmers Reference Guide						bytes(3pm)

NAME
bytes - Perl pragma to force byte semantics rather than character semantics NOTICE
This pragma reflects early attempts to incorporate Unicode into perl and has since been superseded. It breaks encapsulation (i.e. it exposes the innards of how the perl executable currently happens to store a string), and use of this module for anything other than debugging purposes is strongly discouraged. If you feel that the functions here within might be useful for your application, this possibly indicates a mismatch between your mental model of Perl Unicode and the current reality. In that case, you may wish to read some of the perl Unicode documentation: perluniintro, perlunitut, perlunifaq and perlunicode. SYNOPSIS
use bytes; ... chr(...); # or bytes::chr ... index(...); # or bytes::index ... length(...); # or bytes::length ... ord(...); # or bytes::ord ... rindex(...); # or bytes::rindex ... substr(...); # or bytes::substr no bytes; DESCRIPTION
The "use bytes" pragma disables character semantics for the rest of the lexical scope in which it appears. "no bytes" can be used to reverse the effect of "use bytes" within the current lexical scope. Perl normally assumes character semantics in the presence of character data (i.e. data that has come from a source that has been marked as being of a particular character encoding). When "use bytes" is in effect, the encoding is temporarily ignored, and each string is treated as a series of bytes. As an example, when Perl sees "$x = chr(400)", it encodes the character in UTF-8 and stores it in $x. Then it is marked as character data, so, for instance, "length $x" returns 1. However, in the scope of the "bytes" pragma, $x is treated as a series of bytes - the bytes that make up the UTF8 encoding - and "length $x" returns 2: $x = chr(400); print "Length is ", length $x, " "; # "Length is 1" printf "Contents are %vd ", $x; # "Contents are 400" { use bytes; # or "require bytes; bytes::length()" print "Length is ", length $x, " "; # "Length is 2" printf "Contents are %vd ", $x; # "Contents are 198.144" } chr(), ord(), substr(), index() and rindex() behave similarly. For more on the implications and differences between character semantics and byte semantics, see perluniintro and perlunicode. LIMITATIONS
bytes::substr() does not work as an lvalue(). SEE ALSO
perluniintro, perlunicode, utf8 perl v5.18.2 2013-11-04 bytes(3pm)
All times are GMT -4. The time now is 08:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy