Visit Our UNIX and Linux User Community

Working with strings (awk, sed, scripting, etc...)

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working with strings (awk, sed, scripting, etc...)
# 1  
Old 05-25-2019
Working with strings (awk, sed, scripting, etc...)

Hi evrybody
For those who are bored I suggest exercise for tail Smilie
There is "csv" string:

Desired output:
| (A) A | (A,B) B | (A,B,C) C | (A,B,C,D) D | (A,B,C,D,E) E | G

There are no whitespace characters at the beginning and end of the line.
# 2  
Old 05-25-2019
Hi, thanks for the puzzle Smilie

awk '{s=x; for(i=1; i<=NF-1; i++) {s=s (s?FS:"(") $i; $i=s ") " $i " " }}{sub(" $",x); print x,$0}' FS=, OFS='| ' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-25-2019
Well, not sure if this is the most elegant solution:

awk '
    {TMP = $NF
     for (i=NF; i>1; i--)    {sub ("," $i, _)
                              TMP = (i==2?"(":_) $0 (i==2?")":_) " " $(i-1) " | " TMP
     gsub (/([^ ],[^ ]*)+/, "(&)", TMP)
     print "| "  TMP
' FS=,  file
 | (A) A | (A,B) B | (A,B,C) C | (A,B,C,D) D | (A,B,C,D,E) E | G

EDIT: or, a bit more straightforward,

awk -F, '{for (i=1; i<NF; i++) {TMP = TMP DL $i; DL = FS; printf "| (%s) %s ", TMP , $i}; print "| " $NF}' file
 | (A) A | (A,B) B | (A,B,C) C | (A,B,C,D) D | (A,B,C,D,E) E | G

EDIT: revisiting the first proposal, it can be simplified somewhat:

awk '
        {TMP = $NF
         for (i=NF; i>1; i--)   {sub ("," $i, _)
                                 TMP = "(" $0 ") " $(i-1) " | " TMP
         print "| "  TMP
' FS=,  file

Last edited by RudiC; 05-26-2019 at 05:13 AM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-25-2019
@Scrutinizer, @RudiC Thank you for the good examples. I think the last one is optimal. I also have variants for parsing the line is not in length but in width on AWK with using RS="," and not a complicated version on SED. I will share my efforts after a while.

Last edited by nezabudka; 05-25-2019 at 04:49 PM..
# 5  
Old 05-25-2019
One might also try:
awk -F, '
NF {	for(i = 1; i < NF; i++) {
		printf("| (%s", $1)
		for(j = 2; j <= i; j++)
			printf("%s%s", FS, $j)
		printf(") %s ", $i)
	print "| " $NF
}' file

This uses a little more verbose approach to the problem, but produces the same output as Scrutinizer's suggestion except for input lines containing no fields. My code won't give any output for empty input lines; Scrutinizer's code will produce an output line containing a vertical bar, a space, and a newline character for an empty input line.

If you want the output his code produces in that case, my code will do that if you remove the first occurrence of NF in my code. If you don't want the output his code produces n that case, his code will get rid of that line if you change the {sub in his code to NF{sub.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-26-2019
Hello to all. Thanks again for participating.
After the post @Don_Cragun I added explanations to each example
Not very elegant but it just works
It works only with a single line.
it is possible with spaces.

while read -d, P; do
        echo -n "$t| ($T) $P"
        t=" "
done < file
echo -n " | "
grep -o '.$' file

It's simple and don't even need to use "hold spase".
This works with each line separately.
sed -rn 's/.$/ | &/; :1;s/^(\S*)(.),/\1 | (\1\2) \2/;t1;s/^ //p' file

Here in my opinion there is elegance but I doubt the effectiveness of the work
This works with all strings as if one ends with a comma excluding the last
awk 'RT {T = T (T?RS:"(") $1; printf "| " T ") " $1 FS; next} {print "| "$1}' RS=, file

Last edited by nezabudka; 05-26-2019 at 06:04 AM..
# 7  
Old 05-26-2019
Originally Posted by nezabudka
[..]Here in my opinion there is elegance but I doubt the effectiveness of the work
This works with all strings as if one ends with a comma excluding the last
awk 'RT {T = T (T?RS:"(") $1; printf "| " T ") " $1 FS; next} {print "| "$1}' RS=, file

Nice use of RT, however RT is GNU awk only.

Here is another option using RS=, which should work with any POSIX awk:
awk 'p{printf "| (%s) %s ",s p,p; s=s p RS } {p=$1} END{print "| " p}' RS=, file

Of course, these options only work with single lines, otherwise we would need to add NR%c conditions

Last edited by Scrutinizer; 05-26-2019 at 07:47 AM..
This User Gave Thanks to Scrutinizer For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #899
Difficulty: Easy
Programs included in BusyBox can be run simply by adding their name as an argument to the BusyBox executable.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

2. Shell Programming and Scripting

Relocation strings using awk/sed from a index file

Hi All, I'd always appreciate all helps from this website. I would like to relocate strings based on the index number from an index file. Index numbers are shown on the first column in the index file (index.txt) and I would like to relocate "path" based on index numbers. Paths are placed... (11 Replies)
Discussion started by: jypark22
11 Replies

3. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

4. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

5. Shell Programming and Scripting

Search strings and highlight them using Perl or bash/awk/sed

Hi, I have two files: a.doc and b.txt I wish to search the strings from file b.txt in a.doc and want to highlight them in a.doc with different colours using Perl or bash./awk/sed? Please guide me. :) Thanks!!!!! (10 Replies)
Discussion started by: bioinfo
10 Replies

6. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

7. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

8. Shell Programming and Scripting

How can i delete the content between all the occurences of two strings using sed or awk command

Hi. I have to delete the content between all the occurrences of the xml tags in a single file. For example: * The tags <script>.....................</script> occurs more than once in the same file. * It follows tagging rules meaning a start tag will be followed by an end tag. Will not have... (9 Replies)
Discussion started by: satheeshkumar
9 Replies

9. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, , file.out In the first textfile which is called are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

10. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

Featured Tech Videos