The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To remove new line character shihabvk UNIX for Advanced & Expert Users 7 06-18-2009 07:44 AM
How to remove extraneous character vsmurali UNIX for Dummies Questions & Answers 7 03-18-2008 12:57 PM
Search term and output term in desired field Raynon Shell Programming and Scripting 28 03-04-2007 02:34 AM
Create a Term & Run chars on this Term the_tical High Level Programming 1 08-12-2003 09:18 PM
Remove Last Character of Line danhodges99 Shell Programming and Scripting 4 05-21-2003 09:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 03-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Some possibilities:

Code:
cut -d"," -f1
Code:
sed '/,$//'
Code:
awk -F, '{print $1}'
Regards
  #3 (permalink)  
Old 03-19-2008
John Mac John Mac is offline
Registered User
  
 

Join Date: Feb 2005
Location: Edinburgh, Scotland
Posts: 5
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 (permalink)  
Old 03-19-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 11:02 AM..
  #5 (permalink)  
Old 03-19-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by Raynon View Post
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 (permalink)  
Old 03-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Place this command before the code in blue to remove the comma:

Code:
sub(",","",$3)
Regards
  #7 (permalink)  
Old 03-20-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Quote:
Originally Posted by Franklin52 View Post
Place this command before the code in blue to remove the comma:

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

Hi Franklin,

Thks that's clean.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0