How to lineup the column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to lineup the column?
# 1  
Old 01-09-2015
How to lineup the column?

Hi Gurus,

I have a file as below.
Code:
deptnm-appnm-code     -------     s(deptnm-ocode-30-ddd)
                                  s(deptnm-ocode-00-dum)
                                  s(deptnm-appnm-ecode)
deptnm-appnm-code-dld -------     s(deptnm-on-rundt-run)
                                  s(deptnm-appnm-ocodel-su)
                                  s(deptnm-appnm-ecode-dld)
                                  s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum -------     s(deptnm-on-rundt-bp)

I need to change the format as below output.
Code:
deptnm-appnm-code	,s(deptnm-ocode-30-ddd)
			,s(deptnm-ocode-00-dum)
			,s(deptnm-appnm-ecode)
deptnm-appnm-code-dld	,s(deptnm-on-rundt-run)
			,s(deptnm-appnm-ocodel-su)
			,s(deptnm-appnm-ecode-dld)
			,s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum	,s(deptnm-on-rundt-bp)

I tried command:
Code:
awk '{if(NF<3)$3=$1} {print $1, $3}' OFS="," demo.txt

but I got below output
Code:
deptnm-appnm-code,s(deptnm-ocode-30-ddd)
s(deptnm-ocode-00-dum),s(deptnm-ocode-00-dum)
s(deptnm-appnm-ecode),s(deptnm-appnm-ecode)
deptnm-appnm-code-dld,s(deptnm-on-rundt-run)
s(deptnm-appnm-ocodel-su),s(deptnm-appnm-ocodel-su)
s(deptnm-appnm-ecode-dld),s(deptnm-appnm-ecode-dld)
s(deptnm-ocode-50-curcnt),s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum,s(deptnm-on-rundt-bp)

how could I fix this script to get expected result?

Thanks in advance
# 2  
Old 01-09-2015
Code:
[~]$ cat file
deptnm-appnm-code     -------     s(deptnm-ocode-30-ddd)
                                  s(deptnm-ocode-00-dum)
                                  s(deptnm-appnm-ecode)
deptnm-appnm-code-dld -------     s(deptnm-on-rundt-run)
                                  s(deptnm-appnm-ocodel-su)
                                  s(deptnm-appnm-ecode-dld)
                                  s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum -------     s(deptnm-on-rundt-bp)
[~]$ sed -e 's/-/ /g; s/\(s.dept\)/,&/' file
deptnm appnm code                 ,s(deptnm ocode 30 ddd)
                                  ,s(deptnm ocode 00 dum)
                                  ,s(deptnm appnm ecode)
deptnm appnm code dld             ,s(deptnm on rundt run)
                                  ,s(deptnm appnm ocodel su)
                                  ,s(deptnm appnm ecode dld)
                                  ,s(deptnm ocode 50 curcnt)
deptnm appnm code dum             ,s(deptnm on rundt bp)
[~]$

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-09-2015
Quote:
Originally Posted by balajesuri
Code:
[~]$ cat file
deptnm-appnm-code     -------     s(deptnm-ocode-30-ddd)
                                  s(deptnm-ocode-00-dum)
                                  s(deptnm-appnm-ecode)
deptnm-appnm-code-dld -------     s(deptnm-on-rundt-run)
                                  s(deptnm-appnm-ocodel-su)
                                  s(deptnm-appnm-ecode-dld)
                                  s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum -------     s(deptnm-on-rundt-bp)
[~]$ sed -e 's/-/ /g; s/\(s.dept\)/,&/' file
deptnm appnm code                 ,s(deptnm ocode 30 ddd)
                                  ,s(deptnm ocode 00 dum)
                                  ,s(deptnm appnm ecode)
deptnm appnm code dld             ,s(deptnm on rundt run)
                                  ,s(deptnm appnm ocodel su)
                                  ,s(deptnm appnm ecode dld)
                                  ,s(deptnm ocode 50 curcnt)
deptnm appnm code dum             ,s(deptnm on rundt bp)
[~]$

this code works, thanks.

sorry, there is a small issue:
Code:
s/-/ /g

replaced the records as well
Code:
deptnm-ocode-30-ddd to deptnm on rundt bp

I fixed it with
Code:
-e 's/ -------//g

is there a regular expression to fix this?

Last edited by ken6503; 01-09-2015 at 11:35 PM..
# 4  
Old 01-09-2015
Is it really OK for the hyphens in the 1st and last fields to be changed to spaces? Do, you want tabs in the output instead of spaces (as shown in your sample output)?

The following seems to produce the output you originally said you wanted:
Code:
awk '{	f1 = (NF > 1) ? $1 : ""
	tabs = (length(f1) < 8) ? "\t\t\t" : (length(f1) < 16) ? "\t\t" : "\t"
	printf("%s%s,%s\n", f1, tabs, $NF)
}' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-10-2015
Quote:
Originally Posted by Don Cragun
Is it really OK for the hyphens in the 1st and last fields to be changed to spaces? Do, you want tabs in the output instead of spaces (as shown in your sample output)?

The following seems to produce the output you originally said you wanted:
Code:
awk '{	f1 = (NF > 1) ? $1 : ""
	tabs = (length(f1) < 8) ? "\t\t\t" : (length(f1) < 16) ? "\t\t" : "\t"
	printf("%s%s,%s\n", f1, tabs, $NF)
}' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6bin/awk, or nawk.
Hi Don,
As always, thank you very much for your help. this code works perfectly.
# 6  
Old 01-10-2015
An example in Bash...just ignores the --------- field.
Code:
#!/bin/bash

file="./lineup.data"

while read fld1 fld2 fld3
do
    if [[ ${fld1:0:1} == 's' ]]; then
        printf "%-24s ,%s\n" " " "$fld1"
    else
        printf "%-24s ,%s\n" "$fld1" "$fld3"
    fi
done < $file

# output
# ------
# deptnm-appnm-code        ,s(deptnm-ocode-30-ddd)
#                          ,s(deptnm-ocode-00-dum)
#                          ,s(deptnm-appnm-ecode)
# deptnm-appnm-code-dld    ,s(deptnm-on-rundt-run)
#                          ,s(deptnm-appnm-ocodel-su)
#                          ,s(deptnm-appnm-ecode-dld)
#                          ,s(deptnm-ocode-50-curcnt)
# deptnm-appnm-code-dum    ,s(deptnm-on-rundt-bp)

This User Gave Thanks to ongoto For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question