Sponsored Content
Top Forums Shell Programming and Scripting Working with strings (awk, sed, scripting, etc...) Post 303035476 by RudiC on Saturday 25th of May 2019 02:45:07 PM
Old 05-25-2019
Well, not sure if this is the most elegant solution:

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



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


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

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SHTOOL-MKLN.TMP(1)					      GNU Portable Shell Tool						SHTOOL-MKLN.TMP(1)

NAME
shtool-mkln - GNU shtool enhanced ln(1) replacement SYNOPSIS
shtool mkln [-t|--trace] [-f|--force] [-s|--symbolic] src-path [src-path ...] dst-path DESCRIPTION
This is a ln(1) style command. It is enhanced to provide automatic calculation and usage of relative links with the shortest possible path, if possible. Usually if src-path and dst-path are not absolute paths or at least they share a common prefix except the root directory (``"/"''). When more than one src-path is specified, all of them are linked into dst-path. OPTIONS
The following command line options are available. -t, --trace Enable the output of the essential shell commands which are executed. -f, --force Force the creation of the link even if it exists. Default is to fail with error. -s, --symbolic Create a symbolic link instead of a hard-link. EXAMPLE
# shell script shtool mkln -s foo/bar baz/quux HISTORY
The GNU shtool fixperm command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 1998 for ePerl. SEE ALSO
shtool(1), ln(1). 18-Jul-2008 shtool 2.0.8 SHTOOL-MKLN.TMP(1)
All times are GMT -4. The time now is 09:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy