6

[BASH] remove a portion of a string and store it I a new variable

 2 years ago
source link: https://www.codeproject.com/Questions/5315120/BASH-remove-a-portion-of-a-string-and-store-it-I-a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
I'm trying to remove the HH:MM:SS section in the following string example:

Mar 6 01:00:53 2026

The problem is that I cannot go by character position because the text length can change. Example, there could be 16th instead of 6. Therefore I need a way to look for characters immediately before and after the ":" and remove them.

The output should look like this:

Mar 6 2026

What I have tried:

(date1 derived from a previous operation)
Copy Code
echo $date1
Mar 6 01:00:53 2026

date2=`cut -c 7-15 <<< $date1`

echo $date2 
01:00:53 #< -- I was expecting this to be the date minus the HH:MM:SS, but this is the wrong approach anyway because of the variable length.

date2=$(echo $date1 | awk '{print $1 " " $2 " " $4}')
Comments
Thanks so much. This gives me exactly what I need.
You're really close:
Shell
Copy Code
date2=$(echo $date1 | cut -f3 -d\ )
should do the trick. In this case we're telling cut to return the 3rd field (-f3 flag), with field separator space, (-d\ flag ). Note that for the -d flag you need to make the space char significant using the backslash, otherwise the shell will treat the space a an argument separator, and will complain that option -d requires an argument.
I prefer the $( ... ) form of command substitution, since it can be nested e.g. cmd1 $(cmd2 $(cmd3 arg-a arg-b) arg-3 arg-4) arg-x does what you expect: it executes cmd3 and uses the results of that as an argument to cmd2, the output of which is then passed in as an argument to cmd1.
If you know the column positions, you can save a some overhead using the ${parameter:offset:length} Parameter substitution. In you example above echo ${date1:7:7} does what you want, as long as its the first 9 days of the month, which I think is what you were referring to as the "wrong approach". Bash provides several conversions like this, Look at the man page for bash and search for "Parameter Expansion" to find what is available.

Add your solution here

Preview

Existing Members

Sign in to your account

...or Join us

Download, Vote, Comment, Publish.

Your Email   Password  

 

Your Email   Optional Password  

StrengthToo short

 

I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the CodeProject newsletters
When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK