Inserting a field without disturbing field separator on other fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a field without disturbing field separator on other fields
# 15  
Old 05-14-2016
And here is a commented version of my code:
Code:
nawk '	# Start nawk script.
# The following section of code is processed when we are reading the 1st input
# file (i.e., when the current number of lines read (NR) is the same as the
# current number of lines read from the current input file (FNR)).
NR == FNR {
	f19[NR] = $1 	# Gather the values that are to be inserted as the new
			# field #19 in subsequent files.
	next		# Read the next input line without processing the
			# remaining steps in this script.
}

# The following section of code is processed when we see the 1st line in any
# subsequent file...
FNR == 1 {
	# Increment a count of the number of files seen...
	if(fc++)
		# and, if it is not the first one, close the previous output
		# file.
		close(ofn)
	# Calculate the the number of characters in the current input line that
	# appear before the spot where the new field is to be added.  It is
	# assumed that the length of the first field in each line is a constant
	# within any input file (which is true by definition when input files
	# contain only one line).
	len = length($1) + 17 * 7
	# Set the output file name to be used when processing this input file.
	ofn = "output" fc
}
# The following section is processed for each line in the current file.
{	# Print the 1st len characters of the current input line, followed by
	# the value to be inserted as field #19 (as a 7 character floating
	# point value with three digits after the decimal point) in this file,
	# followed by the remainder of the current input line to the current
	# output file.
	printf("%s%7.3f%s\n", substr($0, 1, len), f19[fc],
		substr($0, len + 1)) > ofn
}' 7lines input[1-7]
# The above line terminates the nawk script and names the file containing the
# values to be inserted (which much be first) and the names of the input files
# to be modified in the order you want them to be processed.

Let me know if anything needs further explanation.
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 05-16-2016
Hi Don and Rudi,

Thanks for your time and Thank you so much for a clear explanation on the code.

Regards,
am24
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

2. Shell Programming and Scripting

awk field separator help -

Hi Experts , file : - How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range. I am trying : awk -F"]" '{print $1}' but it is printing the entire file. Not first field. The desired output needed for first field... (9 Replies)
Discussion started by: rveri
9 Replies

3. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

4. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

echo field separator

I am trying to echo all fields except for the last field. I want to include the field seperator, but it is removed. echo "a;s;v;g" | awk -F ";" '{$(NF--)=""; print}' a s v I want an output like this: a;s;v; (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

Field separator in awk

Hi I need to check if field separator I am using in awk statement is " : ", for example: TIME=12:59 HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'` MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'` Is there a way to check within the above awk statement ? Thanks for help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question