Remove First Char from Line in File Only if it's a comma


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove First Char from Line in File Only if it's a comma
# 1  
Old 06-01-2012
Remove First Char from Line in File Only if it's a comma

I have a file, I need to remove the first character of each line, but only if it's a comma. I don't want to delete any other commas in each line.

Trying cat or sed but I really don't know them very well, would love some help.

This removes the first comma, but it removes the first comma no matter where it is in each line.
Code:
cat in.txt | sed 's/,//' > out.txt

Thanks!
Cynthia
# 2  
Old 06-01-2012
what about something like:

Code:
cat in.txt | sed 's/^,//' > out.txt

or

Code:
sed 's/^,//' <in.txt >out.txt

# 3  
Old 06-01-2012
Awesome thanks they both work.
# 4  
Old 06-01-2012
Those two commands are essentially the same.
The second is preferred, as it eliminates a 'cat' command.
# 5  
Old 06-01-2012
Great, I'll use the second, good to be efficient.

My file may have double commas at the beginning of each line, or it may be a single comma. Is there a way to eliminate either the ,, or the , as the first characters in one line, instead of doing this twice?

Thanks.
# 6  
Old 06-01-2012
Change the pattern to /^,,?/
# 7  
Old 06-01-2012
Code:
sed 's/^,\{1,2\}//'

Code:
sed 's/^,//;s/^,//'


--
Remove any number of commas at the beginning of the line:
Code:
sed 's/,*//'


--
@Corona ? is not part of POSIX BRE
This User Gave Thanks to Scrutinizer 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

How to remove comma from first and last line?

Hi Gurus, I need to remove comma from first and last line. I tried below code, but no luck. It only remove first line. Gurus, please help. awk -F"," '{if(NR==1||NR==$NR) print $1; else print $0}' TEST sampe file: ABC HEADER TOTAL RECORDS ARE 2.00,,,,... (13 Replies)
Discussion started by: ken6503
13 Replies

2. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

3. Shell Programming and Scripting

Remove only specific char on every line when exists

Hi I need to remove "|" char when it's the last char of the line. Input file: generoso|desprendido|altruista| abnegar|ceder|sacrificar| abocetado-da|esbozado| apuntado|insinuado|incompleto abocetar|esbozar|bosquejar| diseņar|delinear ------------------------ output need --- ... (11 Replies)
Discussion started by: lookoo
11 Replies

4. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

5. Shell Programming and Scripting

How to remove the # char form a line?

Hi, my file has below details and I want remove the # char from only specific line. #TEST:00:START #TEST1:01:INPROCESS #TEST2:02:ABOUTTO #TEST3:03:COMP i.e if want remove the # from 2nd line then file to be updated as #TEST:00:START TEST1:01:INPROCESS #TEST2:02:ABOUTTO... (6 Replies)
Discussion started by: sandyrajh
6 Replies

6. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

7. Shell Programming and Scripting

Remove special char from end of the file

Hi I am working on a bash script and would know how to use cut or sed to remove (F/.M/d h) from a text file. Before 1 text to save (F/.M/d h) after 1 text to save Thanks in advance (5 Replies)
Discussion started by: pelle
5 Replies

8. UNIX for Dummies Questions & Answers

How to remove comma from the last line of the file

Hi, I have a file which has records which end with a comma. for example: My file looks like 1234, 5678, 3455, 3566, 4444, 9999, I need to remove comma for the last line in the file so that my file should look like: 1234, 5678, 3455, (5 Replies)
Discussion started by: sandeep_1105
5 Replies

9. UNIX for Advanced & Expert Users

remove unnecessary comma from file

HI all, I have a file with following data - test1 "ABC,D",1234,"XYZ,QWER",1234 "SZXA",9870,"ASD,QWERT",234 "XZ,SD",9478,"ADCS,AXZ",876 "WESR",8764,"AQZXAS",9888 "WESR",9898,"WESDRTSAW",3323 I need to get rid of unnecessary commas in fields having double quotes. Ouput - ... (1 Reply)
Discussion started by: sumeet
1 Replies

10. Shell Programming and Scripting

How to remove new line char from a string

Hi Can anyone tell me how can i remove new line character from a string. My requirement is to read a line from a file and store it to a string. read line string1=$line read line string2=$line echo $string1$string2 The result i am getting in different line. i want the output in the same... (1 Reply)
Discussion started by: sreedivia
1 Replies
Login or Register to Ask a Question