gawk to remove last character in a line or string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk to remove last character in a line or string
# 1  
Old 09-28-2012
gawk to remove last character in a line or string

I am outputting a line like this
Code:
print $2 "/" $4

The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this:
Code:
print $2 "/" substr($4, 1, length($4) - 1)

Thanks.
# 2  
Old 09-28-2012
You can use sub or gsub - but it is not any "neater" than any other function, IMO.
Code:
print $2 "/" gsub(":$", "", $4, )

# 3  
Old 09-28-2012
Quote:
Originally Posted by jim mcnamara
You can use sub or gsub - but it is not any "neater" than any other function, IMO.
Code:
print $2 "/" gsub(":$", "", $4, )

gsub returns the number of the substitutions - I don't think that's what the OP is after.
# 4  
Old 09-28-2012
Thanks.

I was hoping there would be something like I would do in shell:
Code:
 echo ${var%?}

or
Code:
 echo ${var%:}

or similar.

---------- Post updated at 11:46 AM ---------- Previous update was at 11:40 AM ----------

Quote:
Originally Posted by vgersh99
gsub returns the number of the substitutions - I don't think that's what the OP is after.
True. He probably meant:
Code:
gsub(":$", "", $4, ); print $2 "/" $4

# 5  
Old 09-28-2012
If $4 contains a number (with or without leading spaces) and then the : at end, you could write something like print $2 "/" $4+0.
# 6  
Old 09-28-2012
Yup, my post was in error. Should be:
Code:
gsub(":$", "", $4 )

print $2 "/" $4

# 7  
Old 09-29-2012
gsub is unnecessary here. sub is enough.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove string between number and character

hello ! I have to remove string between a number and set of characters. For example, 35818 -stress - - -stress - - - - - - DB-3754 44412 caul kid notify DB-3747 54432 roberto -, notify DB-3725 55522 aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search... (7 Replies)
Discussion started by: ManoharMa
7 Replies

2. Shell Programming and Scripting

Bash: How to remove the last character of a string?

In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. (12 Replies)
Discussion started by: LessNux
12 Replies

3. Shell Programming and Scripting

remove the first and last character of a string

How can i remove the first and last character of strings like below: "^^^613*" "admt130" "^^^613*" "123456" "adg8484" "DQitYV09dh1C" Means i wanna remove the quotes(""). Please help (17 Replies)
Discussion started by: proactiveaditya
17 Replies

4. Shell Programming and Scripting

remove all occurrences of a character at the beginning of a string

Hi there, i need some help to remove all occurrences of a certain character at the beginning of a string. Example: my string is 00102030 and i want to remove all zeros from beginning of string so the result is 102030 (3 Replies)
Discussion started by: gigagigosu
3 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. 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

7. Shell Programming and Scripting

Sed is doing my head in! How do you remove the first character of a string?

Hello! Please bare with me, I'm a total newbie to scripting. Here's the sudo code of what I'm trying to do: Get file name Does file exist? If true get length of file name get network id (this will be the last 3 numbers of the file name) loop x 2 If... (1 Reply)
Discussion started by: KatieV
1 Replies

8. Shell Programming and Scripting

How to remove last character in a string read from file

Hello, The last character is a comma , I have tried the following: sed -e 's/\,$//' filename-to-read however - there are still commas at the end of each line...:confused: (5 Replies)
Discussion started by: learning
5 Replies

9. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

10. Shell Programming and Scripting

shell script to remove the last character(.) of a string

hi I have a list of words in a text file. these words are appended by "." at their end. They look something like this. word1. word2. word3. word4. word5. I need to remove the last character "." from all the words. The output must look something like this. word1 word2 word3... (7 Replies)
Discussion started by: ss3944
7 Replies
Login or Register to Ask a Question