Remove last character of a term


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last character of a term
# 1  
Old 03-19-2008
Remove last character of a term

Hi All,

I have a few terms with a comma as the last character.
Can any experts show me how to remove the last character?
Note the the length of the term is not fix.

Input:

1,
2,
12,
14,
103,
198,
3006,



Output,

1
2
12
14
103
198
3006
# 2  
Old 03-19-2008
Some possibilities:

Code:
cut -d"," -f1

Code:
sed '/,$//'

Code:
awk -F, '{print $1}'

Regards
# 3  
Old 03-19-2008
Hi, Raynon -

Two easy ways, asusming you're using a file:

1. awk -F',' '{print $1}' infile > outfile
2. cut -d',' -f1 infile > outfile

I've quoted the comma delimiter in both cases to demonstrate that you should use quotes if the delimiting character could possibly be interpreted by the shell (such as $ or *).

Hope this helps,

John
# 4  
Old 03-19-2008
Thks John and Frankin,

They worked jus fine. !!!
Cool!!

Just a small problem, how can i incorporate above " awk -F',' '{print $1}' " to the below code to the statement in blue?? I am expecting my 3rd field to be smiliar to for eg " 15, " but i only want to capture " 15 ".
Can you help ?

Code:
$1 == "Device" && $2 == "id" {

#print;
unit_no = $3;
flag = 1}

flag > 0 && flag < 4 && $9 == "Device" && $10 == "Sort" {

print flag;
printf ("%s %s\n",unit_no,$0) 
flag++;

}

flag > 3 {flag = 0}


Last edited by Raynon; 03-19-2008 at 12:02 PM..
# 5  
Old 03-19-2008
Quote:
Originally Posted by Raynon
Just a small problem, how can i incorporate above " awk -F',' '{print $1}' " to the below code to the statement in blue?? I am expecting my 3rd field to be smiliar to for eg " 15, " but i only want to capture " 15 ".
Can you help ?

Code:
$1 == "Device" && $2 == "id" {

#print;
unit_no = $3;
flag = 1}

flag > 0 && flag < 4 && $9 == "Device" && $10 == "Sort" {

print flag;
printf ("%s %s\n",unit_no,$0) 
flag++;

}

flag > 3 {flag = 0}

Code:
unit_no=$(echo $3 | awk '{print substr($0,1,match($0,/\,/)-1)}')

# 6  
Old 03-19-2008
Place this command before the code in blue to remove the comma:

Code:
sub(",","",$3)

Regards
# 7  
Old 03-20-2008
Quote:
Originally Posted by Franklin52
Place this command before the code in blue to remove the comma:

Code:
sub(",","",$3)

Regards

Hi Franklin,

Thks that's clean.
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 newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Remove last character from filename

Hi All, I have different type of file (.txt,.csv,.xml) format in my current directory. My requirement is that I need to remove the last character from the file format. Example count.txt$ csp_rules.csv^ Date.xml~ Need Output: count.txt csp_rules.csv Date.xml How to do that?.... (5 Replies)
Discussion started by: suresh01_apk
5 Replies

3. Shell Programming and Scripting

How to remove first 2 character from file name

Hi All Please help me to remove the first 2 character from the file name. files are like this $ ls 12aman file 13atul si 56rana se I want to remove the first 2 char which are numbers. I want the o/p like thus aman file atul si rana se (8 Replies)
Discussion started by: atul9806
8 Replies

4. Shell Programming and Scripting

Remove space before a character

Hi guys, I am new to shell scripting and I have a small problem...If someone can solve this..that would be great I am trying to form a XML by reading a flat file using shell scripting This is my shell script LINE_FILE1=`cat FLEX_FILE1.TXT | head -1 | tail -1` echo... (1 Reply)
Discussion started by: gowrishankar05
1 Replies

5. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

6. Shell Programming and Scripting

Script to remove first character if it is zero

Hi All, I have a input like this. 01 i want the output like this. 1 But if the input file is like 11 i should not do anything. Can anyone please help to get a command to do this. thanks, Giri. ---------- Post updated at 02:11 AM ---------- Previous update was at 02:04 AM ----------... (5 Replies)
Discussion started by: girish.raos
5 Replies

7. UNIX for Dummies Questions & Answers

How to remove \ character

Dear Members, I have a file which is a single line file. It has "\" character and i need to replace this character with a new line character. How can we do this? I tried with sed but it did not work. sed 's//"\n"/g' t1 > t2Thanks Sandeep (3 Replies)
Discussion started by: sandeep_1105
3 Replies

8. Shell Programming and Scripting

Remove a ^M character

Hi, I'd like to ask for some help with the following: I've cut a couple of columns of file1 to create file2 with the following code: cur -f 1,3,8 file1 > file2 Then I need to transfer file 2 from UNIX to Windows and use it further. Unfortunatelly, for some reason the line is displayed... (4 Replies)
Discussion started by: zajtat
4 Replies

9. Shell Programming and Scripting

Search term and output term in desired field

Hi All, I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ? ... (28 Replies)
Discussion started by: Raynon
28 Replies

10. Programming

Create a Term & Run chars on this Term

hi floks ! i'd like to know how can i transmete a character or a string from my source code to a term and make it interpret or un by the shell wich is running in my term. I'd like to create a Term from my code (and get its file descriptor) and then transmete each char typed on the keyboard to... (1 Reply)
Discussion started by: the_tical
1 Replies
Login or Register to Ask a Question