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.
# 8  
Old 05-20-2018
Spelling things wrongly usually is fatal in IT unless specially taken care of in the code. So instead of softening the conditions for your replacement, you might want to install error checking upfront.
Howsoever, for your problem, try
Code:
sed -r "\$s/(([^,],){2}).*/\1$TUNA/g" file
Line 1
Line 2
Line 1
ABC,2,TUNA,PIE

or, if your sed version doesn't allow for extended regexes,
Code:
sed  "\$s/\(\([^,],\)\{2\}\).*/\1$TUNA/g" file

Unfortunately, when using double quotes, you need to escape the $ sign designating the last line of the input stream.
# 9  
Old 05-20-2018
Hi RudiC,

I used you first sample code and it seems to only work with files with fewer lines, i have a file which consists of 40+ lines and it doesn't seem to work. Is there a way to resolve this?

Also would it matter if i have a file that consists of multiple Commasas a delimiter like below? Would it potentially affect the way the script works?

Code:
"FOOD",CAT,20160606,20160606,001
"FOOD1",TUNA,"PASTA"
"FOOD2",CHICKEN,"PASTA"
"FOOD3",HAM,"PASTA"
"FOOD4",EGG,"PASTA"
"FOOD5",NOODLE,"PASTA"
"FOOD6",BACON,"PASTA"
"FOOD7",FISH,"PASTA"
"FOOD8",BIG,"PASTA"
"TOTAL",8,FOOD,ABC1234567

Also apologies in advance if i keep changing the way the code in the file looks it's that i'm trying to keep it as generic as possible.

Last edited by Stinza; 05-20-2018 at 07:11 PM.. Reason: Changes
# 10  
Old 05-21-2018
Quote:
Originally Posted by Stinza
Hi RudiC,

I used you first sample code and it seems to only work with files with fewer lines, i have a file which consists of 40+ lines and it doesn't seem to work. Is there a way to resolve this?

Also would it matter if i have a file that consists of multiple Commasas a delimiter like below? Would it potentially affect the way the script works?

Code:
"FOOD",CAT,20160606,20160606,001
"FOOD1",TUNA,"PASTA"
"FOOD2",CHICKEN,"PASTA"
"FOOD3",HAM,"PASTA"
"FOOD4",EGG,"PASTA"
"FOOD5",NOODLE,"PASTA"
"FOOD6",BACON,"PASTA"
"FOOD7",FISH,"PASTA"
"FOOD8",BIG,"PASTA"
"TOTAL",8,FOOD,ABC1234567

Also apologies in advance if i keep changing the way the code in the file looks it's that i'm trying to keep it as generic as possible.
Generic is one thing. But, without knowing how it doesn't work and without knowing what the last line really looks like in a file where it doesn't work, we are all left guessing with no solid idea of what might be wrong.

If I were to make a bunch of wild guesses, might I be correct in saying that the sed command RudiC suggested in post #8:
Code:
sed  "\$s/\(\([^,],\)\{2\}\).*/\1$TUNA/g" file

works just fine as long as there is exactly one digit in the second field in the last line in file (as found in all of your sample input files), but it doesn't do anything if there is more than one digit in that field? If my wild guess is correct, would the following slight modification of RudiC's code work correctly for you?
Code:
sed "\$s/^\(\([^,]*,\)\{2\}\).*/\1$TUNA/" file

We don't necessarily need the exact contents of the file(s) you're working on to help you get working code, but we do need representative contents that accurately show us the format of the data you're going to be processing with a clear English description of the variations that would be expected in your real data.

Last edited by Don Cragun; 05-21-2018 at 09:37 PM.. Reason: Fix typo: s/know/knowing/
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 05-21-2018
As the sed script is going for the last line in the input stream, it doesn't matter if it's 40 lines or thousands of lines of data.
You may want to anchor the regex (after applying Don Cragun's "patch") at the begin-of-line to be even safer to retain the FIRST two fields.

What do you mean by "multiple Commas as a delimiter like below"? I can't recognize any in your sample...
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