Need to split string on single quote as delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to split string on single quote as delimiter
# 1  
Old 08-27-2019
Need to split string on single quote as delimiter

I have a variable that contains the following string:

Code:
FPATH=-rw-rw-r-- 1 user1 dba 0 Aug  7 13:14 /app/F11.3/app/cust/exe/filename1.exe'  -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql'  -rw-r--r-- 1 user1 dba 0 Aug  6 17:20 /app/F11.2/app/01/mrt/file1.mrt'

I need to split the string based on single quote as delimiter print as below:

Desired Output:
Code:
-rw-rw-r-- 1 user1 dba 0 Aug  7 13:14 /app/F11.3/app/cust/exe/filename1.exe  
-rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql
-rw-r--r-- 1 user1 dba 0 Aug  6 17:20 /app/F11.2/app/01/mrt/file1.mrt

Below is what I tried but it does not work:
Code:
#a=($(echo $FPATH | tr '/'' "\n"))
a=(`echo $FPATH | sed 's/'/\n/g'`)

   for i in "${a[@]}"
do
   printf "ARRAY3:$i"
done

Can you please suggest why is it breaking and how can i get it to work ?

Last edited by rbatte1; 09-04-2019 at 03:14 PM..
# 2  
Old 08-27-2019
Your sed is not quoted properly.

Try tr, which is used for such manipulation, with sed to remove blank begining of line.
Code:
echo $FPATH | tr "'" "\n" | sed "s/^ //g"

Also, you must enclose the content of FPATH variable in double quotes to work.

Please, specify operating system and shell in your future posts.

Hope that helps.
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 3  
Old 08-27-2019
FPATH gets the string by reading it from a file hello.txt.
Code:
input="./hello.txt"
while IFS= read -r line
do
  echo "$line"
IFS=': ' read -r ip string <<< "$line"

Below is how I tried your suggestion.

Code:
a=`echo $FPATH | tr "'" "\n" | sed "s/^ //g"`
   for i in "${a[@]}"
do
   printf '$i' >> output.txt
done

However, due to single quotes printf '$i' we get the output which is enclosed in single quotes.

If I print without single quotes i.e printf $i then the print statement fails becoz of illegal argument like "-r"
Code:
uname -a
Linux mymac 3.10.0-957.12.1.el7.x86_64 #1 SMP Wed Mar 20 11:34:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

# 4  
Old 08-27-2019
Try
Code:
printf -- $i

to eliminate that
Code:
bash: printf: -r: invalid option

problem incorrectly cited by you. Still, your entire code wouldn't work as
- you don't create an array
- even if you created an array by using the a=(...) syntax, it would contain 27 elements to loop across with for.
- even if you had a 3 element array, the ${a[@]} expansion wouldn't supply those three.


Code:
IFS="'" read -a a <<< $FPATH

would create the desired three element array, and e.g. IFS=$'\n'; echo "${a[*]}" would correctly expand it.


Or try
Code:
while read LN; do echo $LN; done <<< $(echo "$FPATH" | tr \' $'\n')

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-27-2019
Quote:
Originally Posted by RudiC
Try
Code:
printf -- $i

to eliminate that
Code:
bash: printf: -r: invalid option

problem incorrectly cited by you. Still, your entire code wouldn't work as
- you don't create an array
- even if you created an array by using the a=(...) syntax, it would contain 27 elements to loop across with for.
- even if you had a 3 element array, the ${a[@]} expansion wouldn't supply those three.


Code:
IFS="'" read -a a <<< $FPATH

would create the desired three element array, and e.g. IFS=$'\n'; echo "${a[*]}" would correctly expand it.


Or try
Code:
while read LN; do echo $LN; done <<< $(echo "$FPATH" | tr \' $'\n')

@RudiC Thank you for the help though it still does not work. Below is the complete snippet.

Code:
#!/bin/bash
input="../vars/hello.txt"
while IFS= read -r line
do
    echo "$line"
    IFS=': ' read -r ip string <<< "$line"
    FPATH="$string"
    FPATH=${FPATH//$'u\''/}
    echo "PRINTING4:$FPATH"
    printf  "\n" >> new_test.txt

    while read LN; 
    do
        echo $LN; >> new_test.txt
     #done <<< $(echo "$FPATH" | tr \' $'\n')
     done <<< $(echo "$FPATH" | tr "'" "\n" | sed "s/^ //g")
done < "$input"

I get blank i.e no entries in the new_text.txt instead of the desired output. even when i try the other suggestion i.e done <<< $(echo "$FPATH" | tr \' $'\n')

Debug shows:
Code:
PRINTING4:-rw-rw-r-- 1 user1 dba 0 Aug  7 13:14 /app/F11.3/app/cust/exe/filename1.exe'  -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql'  -rw-r--r-- 1 user1 dba 0 Aug  6 17:20 /app/F11.2/app/01/mrt/file1.mrt'
+ printf '\n'
++ echo '-rw-rw-r-- 1 user1 dba 0 Aug  7 13:14 /app/F11.3/app/cust/exe/filename1.exe'\''  -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql'\''  -rw-r--r-- 1 user1 dba 0 Aug  6 17:20 /app/F11.2/app/01/mrt/file1.mrt'\'''
++ tr ''\''' '
'
+ read LN
+ echo -rw-rw-r-- 1 user1 dba 0 Aug 7 13:14 /app/F11.3/app/cust/exe/filename1.exe -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql -rw-r--r-- 1 user1 dba 0 Aug 6 17:20 /app/F11.2/app/01/mrt/file1.mrt
-rw-rw-r-- 1 user1 dba 0 Aug 7 13:14 /app/F11.3/app/cust/exe/filename1.exe -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql -rw-r--r-- 1 user1 dba 0 Aug 6 17:20 /app/F11.2/app/01/mrt/file1.mrt
+ read LN
+ IFS=
+ read -r line -rw-r--r-- 1 user1 dba 0 Aug  6 17:20 /app/app/F11.3/app/cust/mrt/file1.mrt'

# 6  
Old 08-27-2019
It works with the below code:

Code:
a=`echo $FPATH | tr "'" "\n" | sed "s/^ //g"`
   for i in "${a[@]}"
do
   printf -- "$i"
done

Thank you everyone for help!!
# 7  
Old 08-27-2019
Another way that doesn't need externals, arrays, loops, or backticks:

Code:
INPUT="string'delimited'with'quotes"
OLDIFS="$IFS" ; IFS="'"
printf "%s\n" $INPUT
IFS="$OLDIFS"

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Enclose String in single quote

I have a comma separated file which contains data like; File header: ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3 File Detail: AMR,5100,Total Services,305,D0120,,, AMR,5101,Periodic Services,40702,H2011,U1,, AMR,5112,Day... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

4. Shell Programming and Scripting

double quote as the delimiter

I'm trying to extract a column from a csv file with either cut or awk but some of the fields contain comma with them: "Field1","Field2, additional info","Field3",...,"Field17",... If I want to extract column 3 and use comma as the delimiter, I'll actually get the additional info bit but not... (4 Replies)
Discussion started by: ivpz
4 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. Shell Programming and Scripting

How to split a string with no delimiter

Hi; I want to write a shell script that will split a string with no delimiter. Basically the script will read a line from a file. For example the line it read from the file contains: 99234523 These values are never the same but the length will always be 8. How do i split this... (8 Replies)
Discussion started by: saint34
8 Replies

7. UNIX for Dummies Questions & Answers

find single quote in a string and replace it

Hi, I have variable inside shell script - from_item. from_item = 40.1'1/16 i have to first find out whether FROM_ITEM contains single quote('). If yes, then that need to be replace with two quotes (''). How to do it inside shell script? Please note that inside shell script........ (4 Replies)
Discussion started by: yogichavan
4 Replies

8. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

9. Shell Programming and Scripting

Multiple characters including single quote in delimiter

Hello, I need to replace the comma to something else between the single quote: 1aaa,bbb,'cc,cc','ddd',1 2aaa,bbb,'ccc','d,d',0 to 1aaa,bbb,'cc<comma>cc','ddd',1 2aaa,bbb,'ccc','d<comma>d',0 Can someone help? Thanks. (2 Replies)
Discussion started by: bgirl
2 Replies

10. Shell Programming and Scripting

split string with multibyte delimiter

Hi, I need to split a string, either using awk or cut or basic unix commands (no programming) , with a multibyte charectar as a delimeter. Ex: abcd-efgh-ijkl split by -efgh- to get two segments abcd & ijkl Is it possible? Thanks A.H.S (1 Reply)
Discussion started by: azmathshaikh
1 Replies
Login or Register to Ask a Question