i have this string:
ex00/{ft_strdup.c} ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/{ft_split.c}
need to remove with sed the curly brackets if there is no comma inside brackets, so desired output:
ex00/ft_strdup.c ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/ft_split.c
to explain what is task of script: i have a txt file and inside it have such lines:
Turn-in directory: ex06/
Files to turn in: ft_this.c, ft_that.h
Turn-in directory: ex07/
Files to turn in: ft_test.c
now I need to create these files with touch
files=$(cat subject.txt \
| grep "Turn-in directory\|Files to turn in" \
| tr -d ' ' \
| awk -F: '{print $2 "}"}' \
| tr -d '\n' \
| sed 's/ex/ ex/g' \
| sed 's/\//\/\{/g' \
| sed 's/{}/{/g')
eval "touch $files"
With your shown samples please try following awk
code. Written and tested in GNU awk
.
awk -v RS='{[^}]*}' '
RT{
if(!sub(/,/,"&",RT)){ gsub(/^{|}$/,"",RT) }
}
{ ORS=RT }
1
END{ print "" }
' Input_file
Using sed
$ sed -E 's/\{([[:alpha:]_.]+)}/\1/g' input_file
touch ex00/ft_strdup.c ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/ft_split.c
Using any sed:
$ sed 's/{\([^,}]*\)}/\1/g' file
ex00/ft_strdup.c ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/ft_split.c
Note that the above will work no matter which characters except ,
, {
, }
, or \n
exist in your file names, eg these are all valid file names:
$ cat file
ex00/{ft_strdup1.c} ex05/{ft-split.c} ex05/{ft=s&pl#it.c}
$ sed 's/{\([^,}]*\)}/\1/g' file
ex00/ft_strdup1.c ex05/ft-split.c ex05/ft=s&pl#it.c
If your file names can contain any of the characters I mentioned above as excluded then ask a new question including those in your sample input/output.
This might work for you (GNU sed):
sed 's/{\([^,]*\)}/\1/g' file
Match globally on a pair of curly braces only when the contents between them does not contain a comma and replace the match by the contents.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.