0

I would like to delete the last string from a Linux/Unix variable containing multiple strings with space separation -

strings='Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL EXCEL'

I want to delete the last string i.e. EXCEL

Expected Output -

Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL

Other Input Set -

str1='RUB 20210607 SPREAD'
str2='RUSSIA NULL NULL NULL 20210607 Y Y Y N N Y Y HTML'
str3='LONDON NULL NULL NULL 20210607 Y Y Y N N Y Y CSV'

PS - Here string is trailing sequence of non-space characters.

4
  • "string" is ambiguous here. I assume you mean the trailing sequence of non-space characters. Commented Jun 7, 2021 at 19:15
  • Possible duplicate showing the 4 primary parameter expansions with substring removal available in Unix shell. Commented Jun 8, 2021 at 7:55
  • @DavidC.Rankin Yes, Parameter Expansion with Substring Removal is helpful for this, I was not aware about it earlier. Commented Jun 8, 2021 at 13:48
  • @chepner - Yes, your assumption is correct. Have updated it in main question definition also. Commented Jun 8, 2021 at 13:49

3 Answers 3

1

The shell can remove or substritute leading or trailing patterns.

strings='Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL EXCEL'
echo "${strings% *}"

prints

Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL

You need the echo command only if you want to print the resulting value. Of course you can use ${strings% *} in the same way as you could use variable expansion, e.g. somecommand $strings vs. somecommand ${strings% *} or somecommand "$strings" vs. somecommand "${strings% *}", depending on your needs.

Citing https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html

${parameter%word}
Remove Smallest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the suffix matched by the pattern deleted.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You, This is what I was looking for, This has solved the problem. Also the link shared is extremely helpful.
0

Hello you can try with awk with the follwing command:

strings='Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL EXCEL'
awk '{$NF="";print}' <<< "$strings"

Output

Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL

Comments

0

You can also get it by making the field separator to " EXCEL".

awk -F " EXCEL" '{print $1}' <<< "$strings"

Output is

Y NULL NULL NULL NULL NULL 20210607 NULL NULL NULL NULL NULL NULL

Edit after clarification from op

Below will work but there are better methods in this thread than my solution. Posting this for some variety.

awk 'BEGIN{ORS=" ";}{for(i=1;i<NF;i++)print $((i));}END{print "\n"}'

3 Comments

Yeah, I appreciate your thought process, However the solutions should be more generic, this solution is more specific. As we can have data like - RUB 20210607 SPREAD RUSSIA NULL NULL NULL 20210607 Y Y Y N N Y Y HTML LONDON NULL NULL NULL 20210607 Y Y Y N N Y Y CSV
@sumitaccess007 Edited my answer and added a method.
Thank you for making it more generic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.