How to modify an output?

Trivia Category: Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

Thread Tools Search this Thread
# 1  
How to modify an output?

I have a list
XPAR    ,
XPAR    ,       ,
XPAR    ,1,
XPAR 196
XPAR 95,77

This has space and tabs on the second row. I would like it to look like

XPAR 1, 196, 95, 77

But I always get the below because of the spaces above.

, , ,1, 196 95 95,77

I use awk -v x="" '{ s=s sprintf(x "%s" x " ", $2) } END { sub(",$2", "", s); print(s) }' sources3 > TMPFILE

Please Help

Last edited by vbe; 02-20-2013 at 12:24 PM.. Reason: code tags for data also...
# 2  
awk -F'[, ]' ' p ~ $1 {
                        for(i=2;i<=NF;i++) {
                                if($i != "")
                                        printf ",%s",$i;
                } p !~ $1 {
                        printf "%s", $1;
                } {
                        p = $1;
                } END {
                        printf "\n";
}' file

This User Gave Thanks to Yoda For This Post:
# 3  
The script bipinajith provided puts out a <comma> before the first XPAR value, uses <comma> instead of <comma><space> to separate XPAR values, and outputs duplicated XPAR values multiple times. I think the following produces the requested output:
awk -F '[ \t,]+' '
{	for(i = 2; i <= NF; i++)
		if($i != "" && (! ($i in x))) {
			printf("%s %s", oc++ ? "," : "XPAR", $i)
END {	printf("\n")}' file

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Thank you Don...It works !!!
# 5  
OR Try

$ awk '{gsub(","," ");
    X[$1]=X[$1]?X[$1]","$i:$1" "$i}}}
    END{print X[$1]}' file

XPAR 1,196,95,77

This User Gave Thanks to pamu For This Post:
# 6  
I would like to also not display the table which contains no info on the second column.
Now i have:

XPAR , ,
XPAR ,1,
XPAR 196
XPAR 95,77

I would like
XPAR 95,77
XPAR 196
Just the lines that contain a data on the second column.
# 7  
Please check this thread

I've already answered there...Smilie

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ed to modify a file --- or not?

Running Oracle Linux 6 (derivative of RHEL 6) Given this snippet of code in a shell script: #-- reset oratab to use 11.2 home for dwdev #-- normally we'd just use sed to do this sort of thing, but that would #-- require permissions that we don't have in the /etc/ directory, so we #-- ... (3 Replies)
Discussion started by: edstevens
3 Replies

2. Solaris

Grant unprivileged user rights to see the output of echo|format but not modify disks

anyone have any idea how do to this with auth_attr? I suspect if I grant him solaris.device.:RO::Device Allocation::help=DevAllocHeader.html that will work but I'm unsure. Just looking for a second opinion. (10 Replies)
Discussion started by: os2mac
10 Replies

3. Shell Programming and Scripting

modify ls -l (long listing format output) strictly using SED only straightforward goalhard 4 me doh

Below is a sample out of ls -l which I would like to rearrange or modify by field numbers for example I successfully managed to disect using simple paragraph however for ls -l I can't divide the rows or fields by field number. Successful modification by fields using SED sample: $ sed -e... (1 Reply)
Discussion started by: wolf@=NK
1 Replies

4. Shell Programming and Scripting

Modify a perl line to parse out and output to another format

Hey there... I am looking for a way to take the below contents ( small excerpt) of this file called PTR.csv ptrrecord,,,,,,False,,,302400,default... (6 Replies)
Discussion started by: richsark
6 Replies

5. Shell Programming and Scripting

Modify file

Hi, I have a file that looks like this: 27+:<10,289808,1> 31+:<11,1445372,1> 33-:<7,1014101,2> 35+:<11,728811,1> 36-:<11,1445205,0> 37+:<11,1445792,2> and I want to change it to this: + 10 289808 + 11 1445372 - 7 1014101 + 11 728811 - 11 1445205 + 11 1445792 (3 Replies)
Discussion started by: kylle345
3 Replies

6. AIX

find command modify the output

Hello All, I am new to this shell scripting , I wanted to modify the output of my find command such that it does not display the path but only file names , for example I am searching for the files which are modified in the last 24 hours which is find /usr/monitor/text/ -type f -mtime... (3 Replies)
Discussion started by: raokl
3 Replies

7. Shell Programming and Scripting

about the modify lines

Hello, I am trying to convert lines from text file, I got lines in text files like that converter.cabWhat i want to do is I want to conver these lines like that, cabwiz.exe extract "C:\1extract\ALPHA" cabwiz.exe extract ... (5 Replies)
Discussion started by: davidkhan
5 Replies

8. Shell Programming and Scripting

Need help with a sh script to spool directory and modify the output (Oracle cnt file)

Hi, I'm creating a shell script to dynamically create a recreate controlfile for an Oracle database. I need to read a cold backup file system, and make some changes to these files. Let's say for argument sake the directory name is /ebsprod_c/oradata and it looks like this:... (6 Replies)
Discussion started by: exm
6 Replies

9. Shell Programming and Scripting

how can i add/modify filename after output?

Hi All, I have a script to convert a file and output the filename with "_output", however it not work. Can help? echo Please input list file name: read listn for file in `cat $listn.txt` do convert $file > $file_output Thanks all!! (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

modify a file

I'm a new member of the forum, I had this new generate file since I use 'grep' and 'awk', what I want to do is get rid off the all 0s before the numbers, is there any one who could help me to figure it out? Thanks a lot! yun 0000000029 000q7472 2002/03/01 0000000030 000q7472 2002/03/01 ... (2 Replies)
Discussion started by: yxiao
2 Replies

Featured Tech Videos