Replace a number in the last line of a delimited file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a number in the last line of a delimited file.
# 1  
Old 04-01-2018
Replace a number in the last line of a delimited file.

Hi all,

I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below :

Code:
Line 1  
Line 2 
Line 3 
ABC|12|4|2

Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer so the result should be 2.

So what i want to do it to write a code that counts the number of rows in the file in this case 4, minus it by 2 (because excluding header and footer), go to the last line, replace the third value seperated by the delimiter and save the file.

Currently i am running the code below:
Code:
awk 'BEGIN{FS=OFS="|"} END{$3=NR-2;print};1' $1

This is the output it get after running the code:

Code:
Line 1  
Line 2 
Line 3 
ABC|12|4|2
ABC|12|2|2

My desired output is :

Code:
Line 1  
Line 2 
Line 3 
ABC|12|2|2

then save it.

can someone help me please ? Hope that all makes sense.

Moderator's Comments:
Mod Comment Added code tags

Last edited by jim mcnamara; 04-01-2018 at 06:01 PM..
# 2  
Old 04-01-2018
If, as shown in your example, only the trailer line contains pipe symbols; try:
Code:
awk '{BEGIN{FS=OFS="|"}NF==4{$3=NR-2}1' "$1"

Otherwise, try:
Code:
awk 'BEGIN{FS=OFS="|"}NR>1{print last}{last=$0}END{$3=NR-2;print}' "$1"

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-02-2018
Thanks Don i used the second code and it worked perfectly!
# 4  
Old 05-16-2018
Hi so now i have another requirement.

Say i have a variable
Code:
ABC=CHICKEN

Code:
Line 1  
Line 2 
Line 3 
ABC|12|4|2
ABC|12|2|BOO

I want to replace Boo with chicken.

Code:
awk 'BEGIN{FS=OFS="|"}NR>1{print last}{last=$0}END{$3=$ABC;print}' "$1"

I have been trying it out like this but it doesn't work is there a way i can replace BOO with CHICKEN
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 05-16-2018 at 09:51 PM.. Reason: Add missing CODE tags.
# 5  
Old 05-16-2018
Code:
awk 'NR>1{print last}{last=$0}END{$NF=ABC; print $0}' ABC="$ABC" FS="|" OFS="|" "$1"

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 05-17-2018
Quote:
Originally Posted by Stinza
Hi so now i have another requirement.

Say i have a variable
Code:
ABC=CHICKEN

Code:
Line 1  
Line 2 
Line 3 
ABC|12|4|2
ABC|12|2|BOO

I want to replace Boo with chicken.

Code:
awk 'BEGIN{FS=OFS="|"}NR>1{print last}{last=$0}END{$3=$ABC;print}' "$1"

I have been trying it out like this but it doesn't work is there a way i can replace BOO with CHICKEN
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.
Your requirements are not at all clear:
  • Saying: "I want to replace Boo with chicken." is strange since "Boo" does not appear anywhere in your sample input and "chicken" is not what is stored in your variable. In BSD-, Linux-, and UNIX-systems strings, variable assignments, filenames, and almost everything else is case sensitive.
  • You can't directly use shell variables inside a single-quoted awk script.
  • Your awk script unconditionally replaces the 3rd field on every line in your input with the contents of the field specified by the numeric value of the awk variable ABC (which, since it hasn't been assigned a value, is 0). How this relates to changing the value of the 4th field (only when it has the value BOO) to CHICKEN is unclear.
If what you are trying to do is to replace every occurrence of the string BOO in your input file with the contents of the shell variable ABC, you might want to try something more like:
Code:
ABC=CHICKEN
awk -v ABC="$ABC" '
BEGIN {	FS = OFS = "|"
}
{	gsub(/BOO/, ABC)
}
1' "$1"

If what you are trying to do is to replace the contents of every field whose value is entirely the case insensitive string BOO with the contents of the shell variable ABC, you might want to try something more like:
Code:
ABC=CHICKEN
awk -v ABC="$ABC" '
BEGIN {	FS = OFS = "|"
}
{	for(i = 1; i <= NF; i++)
		if($i ~ /^[Bb][Oo][Oo]$/)
			$i = ABC
}
1' "$1"

If what you are trying to do is to replace the contents of field $4 with the contents of the shell variable ABC if and only if that field contains the string BOO, you might want to try something more like:
Code:
ABC=CHICKEN
awk -v ABC="$ABC" '
BEGIN {	FS = OFS = "|"
}
$4 == "BOO" {
	$4 = ABC
}
1' "$1"

Of course, all of the above assume that you are using a shell that is based on Bourne shell syntax (not a shell that is based on csh syntax) and that you are not using a Solaris/SunOS system (in which case you would need to use /usr/xpg4/bin/awk or nawk instead of awk).

If none of the above are what you are trying to do and what rdrtx1 guessed is not what you are trying to do then please give a much more detailed, clear description of what your code is supposed to do along with a clear specification of what shell and operating system you're using so we might have a better chance of determining what needs to be done to achieve your desired outcome.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-20-2018
Hi Don,

Apologies for this, my requirements are always changing and i've been trying to learn and implement stuff at the same time so sorry if i haven't been clear enough.

It is Shell based so i will be using AWK, i used your 3rd Sample Code and that work perfectly.

So i have another file that i would like to change.

Take below as a sample code that i currently have that i want to change. So what i want to do is replace everything beyond the second COMMA with whatever i want.

Code:
Line 1
Line 2
Line 1
ABC,2,CHICKEN,PASTA

The below code is my desired output

Code:
Line 1
Line 2
Line 1
ABC,2,TUNA,PIE

The below code is what i am doing currently using to replace this.

Code:
CHICKEN=CHICKEN,PASTA
TUNA=TUNA,PIE
sed "s/$CHICKEN/$TUNA/g" "$1" > temp2

The problem with this is that the code won't if someones accidentally spells something wrong.

So i would like to have a code which:
1. Looks at the bottom line of the file only.
2. Replace everything beyond the second COMMA regardless of whatever is there because i might not necessary know what is there.
3. i want to replace everything beyond the second point with whatever i store in $TUNA.

Please can i get some help.

Thank You

Last edited by Stinza; 05-20-2018 at 08:22 AM.. Reason: Sorted out code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Dummies Questions & Answers

Replace period in a tab delimited file to a number

I have a file like this. It is tab delimited. Unfortunately, the missing data was filled in with a period "." (see the leading lines 1-5 columns) I want to substitute the periods for misisng data with an integer "-999". however, I do not want the global replace to change the other periods seen... (7 Replies)
Discussion started by: genehunter
7 Replies

3. Shell Programming and Scripting

Replace ^M and the new line that follows it in a delimited file

Hello, I want to Replace/Remove ^M and the new line that follows it in a delimited file. So far I have tried following and nothing seems to work for me . Tr –d ‘\r\n’ < old.dat > new.dat -removes all the linefeed, not just the ones after a ^M. Sed ‘/^M$/{N; s/.\n//;}’ < old.dat >... (7 Replies)
Discussion started by: bluestarmoon
7 Replies

4. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

6. Shell Programming and Scripting

Replace col 23 - 26 with new value, non delimited file

hello, i have a undelimited file which contains 229 byte records. i want to change column 23 - 26 with a new value and also change the sign of the data in colulmn 30 - 70. i've tried SED for the first change, but nothing happens: sed 's/\(^.\{22\}\).\{4\}\(.*\)/\0603\2/' inputfile heres an... (8 Replies)
Discussion started by: blt123
8 Replies

7. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

8. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

9. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

10. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies
Login or Register to Ask a Question