Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Splitting a line in two variables

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-18-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
Splitting a line in two variables

Hello.
The file /etc/fstab contains


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

I fill a variable

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

I want

Code:
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 :

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



And other things but I fail.

Any help is welcome.
Sponsored Links
    #2  
Old Unix and Linux 05-18-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 22 October 2017, 11:34 AM EDT
Location: Chennai
Posts: 2,636
Thanks: 580
Thanked 1,254 Times in 1,129 Posts
Hello jcdole,

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

Code:
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:

Code:
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:

Code:
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
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
jcdole (05-19-2017)
Sponsored Links
    #3  
Old Unix and Linux 05-19-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by RavinderSingh13 View Post
Hello jcdole,

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

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

Thanks,
R. Singh
Yes it works.

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

Code:
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 =
Code:
>>>UUID=857c3295-5944-4593-82e2-bb90dede4312 /                     ext4       acl,user_xattr        1 1<<<

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

VAR2=
Code:
>>>acl,user_xattr        1 1<<<

Thank you very much.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting Single line into multiple line diehard Shell Programming and Scripting 2 10-15-2013 07:16 AM
perl : splitting the data into 2 different variables giridhar276 Shell Programming and Scripting 4 09-27-2012 07:38 AM
Splitting the data and storing it into 2 variables jisha Shell Programming and Scripting 10 02-17-2008 09:40 PM
Splitting a line spindoctor UNIX for Dummies Questions & Answers 16 07-10-2007 02:29 PM
Splitting a line up lilas UNIX for Dummies Questions & Answers 1 03-12-2001 11:34 AM



All times are GMT -4. The time now is 11:39 AM.