Adding a "|" character for a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a "|" character for a text file
# 1  
Old 11-03-2011
Adding a "|" character for a text file

Dear Sir,

I am having text file with no delimiter like below
Code:
RAGAV S S             12358     SALES EXECUTIVE            25000
RAJU R B              64253     SALES EXECUTIVE            28000
RUKMAN S              32588     SALES EXECUTIVE            40000
NARGUND S S           12356     SALES EXECUTIVE            50000

I want to add a "|" character in each line in fixed columns (i.e 1st ,20th,30th,50th,60th positions)
the result will be
Code:
|RAGAV S S          |   12358  |   SALES EXECUTIVE    |        25000 |
|RAJU R B           |   64253  |   SALES EXECUTIVE    |        28000 |
|RUKMAN S           |   32588  |   SALES EXECUTIVE    |        40000 |
|NARGUND S S        |   12356  |   SALES EXECUTIVE    |        50000 |

pls help

Last edited by Scott; 11-04-2011 at 05:35 AM.. Reason: Code tags, please...
# 2  
Old 11-03-2011
If the file pattern is as what you have posted, try this...
Code:
sed "s/\(.*\) \([0-9]*\) \(.*\) \([0-9]*\)/|\1|\2|\3|\4|/g" input_file

If the file pattern is different, post the actual data.
What do you mean by 20th position? You need a pipe @ 20th position?

--ahamed

Last edited by ahamed101; 11-03-2011 at 11:38 PM..
# 3  
Old 11-03-2011
I assumed data was already in fixed columns. Save this to a file named something like add_pipe.ksh and invoke with the list of columns where you'd like a pipe symbol supplied on the command line. It automatically adds one at the beginning and ending of the line.

Code:
#!/usr/bin/env ksh

awk -v list="$@" '
BEGIN {
        n = split( list, col, " " );
    }
    {
        last = 1;
        printf( "|" );
        for( i = 1; i <= n; i++ )
        {
            printf( "%s | ", substr( $0, last, col[i]-1 ) );
            last = col[i]+1;
        }
        printf( "%s|\n", substr( $0, last ) );
    }
'

Invoke like this:
Code:
add_pipe.ksh 10 25 50 62

to replace the caracter at character positions 10, 25, 50 and 62 with " | ". Per your example, it adds spaces.
# 4  
Old 11-04-2011
Below would be the output if pipes placed at the positions you required
Code:
~/home>sed s'/^\(.\{19\}\)\(.\{9\}\)\(.\{19\}\)\(.\{9\}\)/|\1|\2|\3|\4|/' inputfile
|RAGAV S S          |   12358 |    SALES EXECUTIVE|         |   25000
|RAJU R B           |   64253 |    SALES EXECUTIVE|         |   28000
|RUKMAN S           |   32588 |    SALES EXECUTIVE|         |   40000
|NARGUND S S        |   12356 |    SALES EXECUTIVE|         |   50000

# or else try
~/home>sed s'/^\(.\{19\}\)\(.\{9\}\)\(.\{19\}\)\(.\{9\}\)\(.*\)$/|\1|\2|\3|\4|\5 |/' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All It's me again with another huge txt files. :confused: What I have: - I have 33 huge txt files in a folder. - I have thousands of line in this txt file which contain many the letter "x" in them. - Some of them have more than one "x" character in the line. What I want to achieve:... (8 Replies)
Discussion started by: Nexeu
8 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

5. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

"vi" text editor character encoding?

Hi! I've got a shell account on a FreeBSD machine. It doesn't have 'vim' installed, but only the original 'vi' text editor ("Version 1.79 (10/23/96) The CSRG, University of California, Berkeley.") So, in PuTTY I've chosen "UTF-8 translation" to have my non-english characters appear correctly.... (2 Replies)
Discussion started by: Gew
2 Replies

8. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies
Login or Register to Ask a Question