Format input file in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format input file in unix
# 1  
Old 10-15-2012
Error Format input file in unix

Dear Freinds,

I have a file a input file as below

Code:
KEY|COLUMN_NAME|SOURCE_VALUE|TARGET_VALUE
1  | CITY      | JUKIL      |JKHJKDH
1  | NUMBER    | 3          |9
1  | JULU      |i8          |i10
2  | CITY      | HDJ        |HIK
2  | JULU      |i20         |i30
3  | CITY      | JUIL       |JKHDH
3  | NUMBER    | 10         |11

Expected output:
Code:
KEY=1| CITY=JUKIL,JKHJKDH |NUMBER=3,9 | JULU=18,i10
KEY=2| CITY=HDJ,HIK       |JULU=i20,i30
KEY=3| CITY=JUIL,JKHDH    |NUMBER=10,11

Plz help
# 2  
Old 10-15-2012
try

Code:
awk -F "|" 'NR>1{if(a[$1]){a[$1]=a[$1]"|"$2"="$3","$4}else{a[$1]="KEY="$1"|"$2"="$3","$4}}END{
for(i in a) { print a[i]}}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-15-2012
@Pamu, Thanks for the reply. It is working as expected. Could you please explain the code ?
# 4  
Old 10-15-2012
Code:
awk -F "|" 'NR>1                         # we check for lines where except first line

{if(a[$1]){                             # Here check if a[$1] is already present or not.

a[$1]=a[$1]"|"$2"="$3","$4}             # If a[$1] presents then append the $2,$3 and $4 to a[$1]

else{a[$1]="KEY="$1"|"$2"="$3","$4}}     # If a[$1] is not present then a[$1] is initilised as required. Just assume $1 = 1,1,1,2,2,3,3 in this case.

END{for(i in a) { print a[i]}}' file     # Here after all the assigning part is done. Get all the index from the a and print a[i]. 

for(i in a)    # Give you all the indexes present in array like here i is 1,2,3

Hope this helps youSmilie
This User Gave Thanks to pamu For This Post:
# 5  
Old 10-15-2012
Simply Amazing. Thanks Pamu Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. UNIX for Dummies Questions & Answers

Format txt file in UNIX

Hello All, I have File_all.txt which have content as below Volume in drive E is Data Volume Serial Number is 586D-6932 Directory of E:\mydi2\siudfor\TFG058 03/27/2014 09:59 PM 5,569 FX\FX01 my4_pg4_MON_20140327_C.zip 1 File(s) 5,569 bytes ... (5 Replies)
Discussion started by: kumar30213
5 Replies

3. Shell Programming and Scripting

Reading input from web into UNIX file

Hi, Could someone let me know how to read input from a web page into a unix file. I am writing my script in bash shell in Solaris version. Thanks in advance, ayarlaga. (7 Replies)
Discussion started by: ayarlaga
7 Replies

4. UNIX Desktop Questions & Answers

Format csv file using Unix

Hi All, I have an csv file with three rows, where first containing header deatils. is there any way to make the first row to appear bold using UNIX command. Input File: Name Rank arun 1 babu 2 Expected Output: Name Rank arun 1 babu 2 (7 Replies)
Discussion started by: arunmanas
7 Replies

5. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

6. Shell Programming and Scripting

Format a file through unix script

Hi, Can you please help me out about how to format a .csv file through unix script? Set of complete commands would be useful or also share few links if available. ---------- Post updated at 09:00 PM ---------- Previous update was at 08:59 PM ---------- I am talking about in terms of... (12 Replies)
Discussion started by: amit.mathur08
12 Replies

7. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (2 Replies)
Discussion started by: Samtel
2 Replies

10. UNIX for Dummies Questions & Answers

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (1 Reply)
Discussion started by: Samtel
1 Replies
Login or Register to Ask a Question