Typical problem in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Typical problem in UNIX
# 1  
Old 09-18-2014
Typical problem in UNIX

Input file
I have a file with four fields.
Code:
f1,f2,f3,f4
A,1,10,00,S
B,2,20,00,00,D
C,3,100,00,00,G

I want Output like
Code:
f1|f2|f3|f4
A|1|10,00|S
B|2|20,00,00|D
C|3|100,00,00|G

please help on this

Last edited by Scrutinizer; 09-18-2014 at 07:50 AM.. Reason: ICODE -> CODE tags
# 2  
Old 09-18-2014
Replace all commas that are followed by a non-0 character:
Code:
sed 's/,\([^0]\)/|\1/g' file

[^0] is the non-0 character. It is in \( \) so can be placed back with \1.
# 3  
Old 09-18-2014
precreate field 3
Code:
perl -ne '@r=split/,/,$_;print join("|",@r[0,1],join(",",@r[2..$#r -1]),$r[$#r])' tmp.dat
f1|f2|f3|f4
A|1|10,00|S
B|2|20,00,00|D
C|3|100,00,00|G

# 4  
Old 09-18-2014
The important thing that the OP needs to specify is what is the criterion?

Because that determines what is a valid solution. For example: if it is "do not change the field separator for occurrences of ,00":
Code:
awk '{for(i=1; i<=NF; i++) gsub(/,/,"|",$i)}1' FS=,00 OFS=,00  file

But if the idea is to only do it after the first two fields and before the last field:
Code:
sed 's/,/|/; s/,/|/; s/,\([^|]*\)$/|\1/' file

# 5  
Old 09-18-2014
Are we after replacing the first, second and last comma with a pipe?

You need to explain more about the context so suggestions will be suitable and leave you able to support it in future.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to compile a software for a non-typical platform?

I am quite new to compiling source codes in linux and have been running into a lot of problems in trying to do so since the platform configuration is different from most. For starters, I know that you need to enter the following commands in order to install any software manually in linux:... (2 Replies)
Discussion started by: Ice_Drake1
2 Replies

2. UNIX for Dummies Questions & Answers

Typical steps to be followed while applying an application patch upgrade on linux

what are the typical steps used by system adminstrators while applying an application patch upgrade (1 Reply)
Discussion started by: ramky79
1 Replies

3. Emergency UNIX and Linux Support

Calculating total space in GB for all files with typical pattern

Hi Experts, In a particular dir, I have many files *AJAY*. How can I get total size of all such files. I tried du -hs *AJAY* but it gave me individual size of all files. All I require is summation of all. Thanks, Ajay (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

4. Shell Programming and Scripting

Guidance needed for a typical shell script with sql query

Hi , I have a txt file with contents like: 1234 2345 3456 7891 I need to write a script which takes input file as txt file..run a sql query for that number and place the output of query in another file.. select * from bus_event where acct_nbr='1234'( from input txt file) the query... (20 Replies)
Discussion started by: Rajesh Putnala
20 Replies

5. Shell Programming and Scripting

A typical array script

Hi All, I need to store the output of "find ." to an array one by one. Output of find . in my case will look like :- . ./one ./one/a ./one/b ./one/c ./two So my first array element should be "/one" and second one "/one/a" (need to remove "." from the output as well). Then I need to... (11 Replies)
Discussion started by: Renjesh
11 Replies

6. UNIX for Dummies Questions & Answers

Meaning and typical use of -3 signal in kill

Hi, What is the use of the signal -3 in kill command in unix? I read the meaning and typical use of this signal in one of the Oreilly books as below. Quit -- stop running (and dump core). Sent when you type CTRL-\. what does the CTRL-\ command do? Is it the combination of CTRL and... (6 Replies)
Discussion started by: venkatesht
6 Replies

7. Shell Programming and Scripting

typical mail script

hi i have a requirement to write a mail script which needs to be automated.There are 7 CSV files generated for 7 clients in a single day.Each file will contain one header and the name of the file follows a nomenclature like ABC_20080402_ClientID.csv.ClientID is lets say... (3 Replies)
Discussion started by: dr46014
3 Replies

8. Solaris

Typical way to disable a dameon

I want to disable some services starting automatically while system booting, for instance if i want to disable vold what i have to do ? i think some services related to a script init level shell directory,and i think as well that since solaris 10 they added a command to enable and disable services... (2 Replies)
Discussion started by: XP_2600
2 Replies
Login or Register to Ask a Question