Splitting a line in two variables

Hello.
The file /etc/fstab contains

UUID=957c3295-9944-1593-82e2-2b90dede4312 /                     ext4       acl,user_xattr        1 1

I fill a variable

SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 )

I want

PART1=>>>>>UUID=957c3295-9944-1593-82e2-2b90dede4312 /                     ext4       <<<<<
PART2=>>>>>acl,user_xattr        1 1<<<<<<

And blank preserved.
The token to split the line is a word. In that example it is "ext4"
PART2 begin with the first non blank character after the delimiter (ext4)
I have try :

PART1=$(echo `expr match "$SOME_LINE" '\(.ext4\)'`)
PART2=$(echo `expr match "$SOME_LINE" '.*\(ext4*\)'`)

And other things but I fail.

Any help is welcome.

Hello jcdole,

Could you please try following and let me know if this helps you.

VAR1=$(grep -oE '.*ext4[[:space:]]+'  Input_file)
VAR2=$(grep -oE 'acl.*[[:space:]]+' Input_file)

Now be very careful here, if you want to preserve space at last here then print them with " like as follows.

Following should be done as per your requirement:

echo "$VAL1" | cat -e
UUID=957c3295-9944-1593-82e2-2b90dede4312 /                     ext4       $

echo "$VAL2" | cat -e
acl,user_xattr        1 $

So above is your requested output, in case you simply do a echo then you could see difference in outputs.

Following should not be done as per your requirement:

echo $VAL2 | cat -e
acl,user_xattr 1$
  
echo $VAL1 | cat -e
UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4$

I hope this helps you.

NOTE: Remove cat -e from above commands as I have shown you to see the space position for variables only.

Thanks,
R. Singh

1 Like

Yes it works.

I have modified VAR2 to have the complete end of the string :

MY_TOKEN=$( cat /etc/fstab | grep \/\..*ext4 | grep acl,user_xattr )
VAR1=$( echo "$MY_TOKEN" | grep -oE '.*ext4[[:space:]]+' )
VAR2=$( echo "$MY_TOKEN" | grep -oE 'acl.*' )
echo ">>>$MY_TOKEN<<<"
echo ">>>$VAR1<<<"
echo ">>>$VAR2<<<"

MY_TOKEN =

>>>UUID=857c3295-5944-4593-82e2-bb90dede4312 /                     ext4       acl,user_xattr        1 1<<<

VAR1=

>>>UUID=857c3295-5944-4593-82e2-bb90dede4312 /                     ext4       <<<

VAR2=

>>>acl,user_xattr        1 1<<<

Thank you very much.