How to change comma delimeter in the file to number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change comma delimeter in the file to number?
# 1  
Old 06-24-2018
How to change comma delimeter in the file to number?

I have a file
Code:
H,20180624200732,VPAS,TRANS_HDR,20180724,
VPAS.TRANS_HDR.20180724.01.txt, ,93,
T,1,

I have to change and instead first comma put ",1" like below


Code:
H,20180624200732,VPAS,TRANS_HDR,20180724,
VPAS.TRANS_HDR.20180724.01.txt,1,93,
T,1,

I made
Code:
sed "2s/, /,1/" $CONTROLFILE

and it put nothing to the file
I tried

Code:
sed "2s/, /,1/" $CONTROLFILE > $CONTROLFILE

In this case the file is empty


Please advise what can I do to have "....,1,...." in the file in second line

Everything I do from the shell script. let me know if I have to put all source code


Thanks for contribution
# 2  
Old 06-24-2018
Hello digioleg54,

Could you please try following and let me know if this helps you.(This should substitute first occurrence of , in 2nd line with ,1,)
Code:
 sed '2s/, ,/,1,/1'  Input_file

In case you want to save output into Input_file itself use sed -i option in above code OR if you want to take backup of Input_file before saving output into it then use sed -i.bak in above code too.

Thanks,
R. Singh
# 3  
Old 06-24-2018
Quote:
Originally Posted by RavinderSingh13
Hello digioleg54,

Could you please try following and let me know if this helps you.(This should substitute first occurrence of , in 2nd line with ,1,)
Code:
 sed '2s/, ,/,1,/1'  Input_file

In case you want to save output into Input_file itself use sed -i option in above code OR if you want to take backup of Input_file before saving output into it then use sed -i.bak in above code too.

Thanks,
R. Singh



Above mentioned -i option is not working


Code:
sed -i '2s/, ,/,1,/1' $CONTROLFILE

gives error below
Code:
sed: 1: "VPAS.TRANS_HDR.20180724 ...": invalid command code V


I am working on iMac terminal. The value of $CONTROLFILE = VPAS.TRANS_HDR.20180724.01.ctl


Thanks for contribution

Last edited by digioleg54; 06-24-2018 at 09:54 PM..
# 4  
Old 06-24-2018
Quote:
Originally Posted by digioleg54
Above mentioned -i option is not working


Code:
sed -i '2s/, ,/,1,/1' $CONTROLFILE

gives error below
Code:
sed: 1: "VPAS.TRANS_HDR.20180724 ...": invalid command code V

Issues like this are why you should always tell us what operating system and shell you're using when you start a new thread. The code Ravinder suggested will work perfectly well on many systems, but since the -i option to sed is not required by the standards, it doesn't work on all systems.

The following should work on any standards conforming system if you're using a shell that conforms to the standards...
Code:
tmpf=./${0##*/}.$$

sed '2s/, /,1/' "$CONTROLFILE" > "$tmpf" && mv "$tmpf" "$CONTROLFILE"
rm -f "$tmpf"

# 5  
Old 06-24-2018
Quote:
Originally Posted by Don Cragun
Issues like this are why you should always tell us what operating system and shell you're using when you start a new thread. The code Ravinder suggested will work perfectly well on many systems, but since the -i option to sed is not required by the standards, it doesn't work on all systems.

The following should work on any standards conforming system if you're using a shell that conforms to the standards...
Code:
tmpf=./${0##*/}.$$

sed '2s/, /,1/' "$CONTROLFILE" > "$tmpf" && mv "$tmpf" "$CONTROLFILE"
rm -f "$tmpf"


Sorry, But I said that option -i is not working for me.

In your line the ksh shell script doesn't understand "mv" command.


Any advise on this?


Thanks for contribution

---------- Post updated at 09:42 PM ---------- Previous update was at 09:14 PM ----------

OK, I changed

Code:
"mv" to 
mv $tmpf $CONTROLFILE

Now ot works.

Thanks a lot for your help


Thanks a lot for your help[/QUOTE]
# 6  
Old 06-25-2018
Quote:
Originally Posted by digioleg54
Sorry, But I said that option -i is not working for me.

In your line the ksh shell script doesn't understand "mv" command.


Any advise on this?


Thanks for contribution

---------- Post updated at 09:42 PM ---------- Previous update was at 09:14 PM ----------

OK, I changed

Code:
"mv" to 
mv $tmpf $CONTROLFILE

Now ot works.

Thanks a lot for your help


Thanks a lot for your help
I have no idea what you're talking about!

The code I gave you didn't have mv in double quotes.

If you're saying you removed double quotes from the arguments to mv as in:
Code:
mv $tmpf $CONTROLFILE

instead of:
Code:
mv "$tmpf" "$CONTROLFILE"

you have made the code much less safe. And, making it less safe has absolutely nothing to do with ksh finding mv!!!

One might guess that you're using a WYSIWYG pretty-printing editor of some sort instead of using a text editor that uses ASCII letters and quoting characters that are required when producing shell scripts.

Please do the following:
  1. Tell us WHAT operating system (name and release number) are you using?
  2. Tell us WHAT shell (name and version) are you using?
  3. Tell us WHAT editor are you using to produce your shell script?
  4. Show us the exact text that is included in your shell script (in CODE tags)!
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-25-2018
And do so in each and every NEW thread!
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

3. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

4. Shell Programming and Scripting

missing comma delimeter in columns

hi if comma delimeter missing in columns treat them as bad file and if it is not then gudfiles. only checking columns not data. id,name,sal,deptno =======> gudfile 1,awa,200,10 2,aba,100,20 3,cdc,300,30 idname,sal,deptno ========> badfile since its missing (.)... (8 Replies)
Discussion started by: awais290
8 Replies

5. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

6. Shell Programming and Scripting

Needed value after the last delimeter in a file with varying number of delimited columns

Hi All, My file has the records as below: aaa\bbb\c\dd\ee\ff\gg zz\vv\ww pp\oo\ii\uu How can I get the value after the last delimeter. My o/p: gg ww uu Thanks in Advance, (5 Replies)
Discussion started by: HemaV
5 Replies

7. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

8. Shell Programming and Scripting

Change the File name using pattern and number

Hi, i have the below files in the wrk folder. File names are AAB0001.R BBA0002.R CCDC0003.R AAB0002.R AAB0034.R BBA0081.R DDDE0008.R i need to change the file name USING pattern and number. output like below :wall: AAB0001.R AAB0002.R AAB0003.R BBA0001.R BBA0002.R CCDC0001.R... (3 Replies)
Discussion started by: krbala1985
3 Replies

9. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

10. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies
Login or Register to Ask a Question