LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question on Cut (https://www.linuxquestions.org/questions/linux-newbie-8/question-on-cut-4175495679/)

maddyfreaks 02-20-2014 02:01 PM

Question on Cut
 
Hi Team.

Got a question

I have variable AB.16.89

i need to change it to AB1689 not sure how to change it using cut (or) awk.

SYIN=AB.16.89

echo $SYSIN|cut -d '.' -f * ???

metaschima 02-20-2014 02:11 PM

I would use tr or sed to simply delete the '.'s

Isaac Velando 02-20-2014 02:13 PM

If all you're trying to do is remove the periods then arguably the most appropriate tool (though as with most text manipulation tasks in bash not the only one) for the job is sed:
Code:

man sed
sed let's you use regular expressions to manipulate text in files or an output stream, so for instance:
Code:

echo $SYSIN | sed 's/\.//g'
The sed syntax used here has the form:
Code:

's(search and replace)/regex pattern to match/replacement/g(replace all matches, not just one)'
In the example I gave the period is escaped since otherwise periods match any character, and it replaces it with nothing (i.e. removes it).

Habitual 02-20-2014 02:13 PM

Code:

echo AB.16.80 | sed 's/\.//g'
slow typer. Thanks Issac!

maddyfreaks 02-20-2014 02:22 PM

thanks solved my proble

metaschima 02-20-2014 03:51 PM

Here's the tr solution
Code:

echo AB.16.80 | tr -d '.'

colucix 02-20-2014 03:59 PM

Since the string is assigned to a variable, I suggest a simple parameter substitution:
Code:

SYSIN=${SYSIN//./}

Firerat 02-20-2014 04:00 PM

If you arw using bash, then use bash ;)


Code:

MyVar=AB.16.80
MyVar=${MyVar//./}

For more infom do a search for
"Bash string manipulation"


All times are GMT -5. The time now is 07:03 AM.