Splitting a line in two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a line in two variables
# 1  
Old 05-18-2017
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.
# 2  
Old 05-18-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-19-2017
Quote:
Originally Posted by RavinderSingh13
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting CSV into variables then to XML file

I have a text file that looks like this: FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like... (15 Replies)
Discussion started by: jeffs42885
15 Replies

2. Shell Programming and Scripting

Splitting one line into multiple lines

Hi, I have one problem scenorio as below. my source file is : cat input_file. "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou." my output should be like. "hi","there","how","are","you?", "It","was","great","working","with",... (7 Replies)
Discussion started by: abhilash_nakka
7 Replies

3. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

4. Shell Programming and Scripting

perl : splitting the data into 2 different variables

I have a perl variable which contains the below value. $var1 = "2% / 51%" Now I would like to split the data into 2 different variables. For example $part1 = 2 $part2 = 51 Could anyone please help me in this regard ? Regards, GS (4 Replies)
Discussion started by: giridhar276
4 Replies

5. Shell Programming and Scripting

Splitting 12M line file

I have a 12M line file that I need to split into smaller files but I'm getting a csplit: virtual memory exhausted error How can I quickly split this file? This is sze -r-xr-xr-x+ 1 VERGE Domain Users 1158288000 May 4 16:31 inputfile This is the command I've tried... csplit... (4 Replies)
Discussion started by: verge
4 Replies

6. Shell Programming and Scripting

Splitting the line in multiple lines

Hi Guys, I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too. I have to split the lines if line is ... (3 Replies)
Discussion started by: dd_sh
3 Replies

7. Shell Programming and Scripting

Splitting the data and storing it into 2 variables

Hi, I have a file in the given format: |aaa |hhh |fat hat chat |make sure I need to get store in a variable,say 'error' the value of the string 'fat hat chat' In short, echo $error should give the result fat hat chat Is this possible using awk? Also, can this be split.For... (10 Replies)
Discussion started by: jisha
10 Replies

8. UNIX for Dummies Questions & Answers

Splitting a line

I have a series of .txt files, that contain lines of text separated by the following string ==================== In some of the .txt files, the string ends with the word Document, leaving the string ==================Document. I would like to be able to split any such line and move the word... (16 Replies)
Discussion started by: spindoctor
16 Replies

9. UNIX for Dummies Questions & Answers

Line Splitting

Hi, I want to make a new line between for eg: 0102030405 to make it look like: 01 02 03 04 05 by using sed commands. I'm just wondering how would I do this? p.s i'm new to this unix programming. (7 Replies)
Discussion started by: evoGage
7 Replies

10. UNIX for Dummies Questions & Answers

Splitting a line up

9600012301F TF02FT T03FFTF04TF 05T FF06TTTT I have to split this line up into this 96000123 01F TF 02FT T 03FFTF 04TF 05T FF 06TTTT Can anyone please Help?? (1 Reply)
Discussion started by: lilas
1 Replies
Login or Register to Ask a Question