Sponsored Content
Top Forums UNIX for Dummies Questions & Answers change field separator only from nth field until NF Post 302688135 by beca123456 on Friday 17th of August 2012 09:47:35 PM
Old 08-17-2012
change field separator only from nth field until NF

Hi !

input:
Code:
111|222|333|aaa|bbb|ccc
999|888|777|nnn|kkk
444|666|555|eee|ttt|ooo|ppp

With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records).

In order to get:
Code:
111|222|333|aaa; bbb; ccc
999|888|777|nnn; kkk
444|666|555|eee; ttt; ooo; ppp

I tried something like:
Code:
gawk 'BEGIN{FS=OFS="|"}{for(i=5; i<=NF; i++) $4 = $4 ($4?"; ":"")$i}1' input

It works for the 4th field, but it also prints the original fields from $5 to $NF after:
Code:
111|222|333|aaa; bbb; ccc|bbb|ccc
999|888|777|nnn; kkk|kkk
444|666|555|eee; ttt; ooo; ppp|ttt|ooo|ppp

Is there any way to to create an array from $4 until $NF without knowing the number of fields (a[$4...$NF])???

Thanks for your help !
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change field separator of grep from : to space

Hi, All, I wonder how to change the field separator of grep from : to space when use grep to display. for example when I use grep -e 'pattern1' -e 'pattern2' -n filename to find patterns, it use : to separate patterns, but I want to use blank space. is there an option I can set to... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies

2. Shell Programming and Scripting

Field separator Ques.

Hello... Im trying to use "- " as field separator... I used awk -F"- " '{print $3}' input_file ... but it's not working, it assumes that the field separator is "-" and not "- " ... Any ideas ?? :( Thanks (6 Replies)
Discussion started by: yahyaaa
6 Replies

3. Shell Programming and Scripting

field separator in Perl

is there a similar parameter you can set in perl like FS in awk? I think I've read all the tutorials on the subject, but cannot get this map split and so on thing to work. I need to sort a file by columns, eg. first, third, fifth... The script I need to add this column sorting is this: use... (38 Replies)
Discussion started by: ahsog
38 Replies

4. Shell Programming and Scripting

dynamically change awk Field Separator FS

Hi All, I was wondering if anyone knew how to dynamically change the FS in awk to accept vairiable containing a field separator. the current code is as below and does not work when i introduce the dynamic FS change :-( validate_source_file() { source_file=$1 ... (2 Replies)
Discussion started by: satnamx
2 Replies

5. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

6. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

7. Shell Programming and Scripting

awk field separator help -

Hi Experts , file : - How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range. I am trying : awk -F"]" '{print $1}' but it is printing the entire file. Not first field. The desired output needed for first field... (9 Replies)
Discussion started by: rveri
9 Replies

8. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

9. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

10. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies
join(1) 						      General Commands Manual							   join(1)

NAME
join - Joins the lines of two files SYNOPSIS
Current syntax join [-a file_number | -v file_number] [-e string] [-o number.field,...] [-t character] [-1 field] [-2 field] file1 file2 Obsolescent syntax [join] [-a number] [-e string] [-j number | field | number field] [-o number.field,...] [-t character] file1 file2 The join command reads file1 and file2 and joins lines in the files that contain common fields, or otherwise according to the options, and writes the results to standard output. STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: join: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
Joins on the fieldth field of file1. Fields are decimal integers starting with 1. Joins on the fieldth field of file2. Fields are decimal integers starting with 1. Produces an output line for each unpairable line found in file1 if number is 1, or file2 if number is 2. With- out -a, join produces output only for lines containing a common field. If both -a 1 and -a 2 are used, all unpairable lines will be out- put. Replaces empty output fields with string. Joins the two files on field of file number, where number is 1 for file1 or 2 for file2. If you do not specify number, join uses field in each file. Without -j, join uses the first field in each file. The default value for both number and field is 1. (Obsolescent) If you enter only a 1 or a 2 as an argument to -j, join interprets this argument as the file number (number); integers greater than 2 are interpreted as the field number (field). Therefore, if you want to specify a field number of 2, you must precede this speci- fication with a number argument; otherwise, the join program interprets the 2 as the file number (number). Produces output lines consisting of the fields specified in one or more number.field arguments, where number is 1 for file1 or 2 for file2, and field is a field number. Multiple -o arguments should be separated with commas. Uses character (a single character) as the field separator character in the input and the output. Every appearance of character in a line is significant. The default separator is a space. If you do not specify -t, join also recognizes the tab and newline characters as separators. With default field separation, the collating sequence is that of sort -b. If you specify -t, the sequence is that of a plain sort. To specify a tab character, enclose it in '' (single quotes). Produces an output line for each unpairable line in file_number (where file_number is 1 or 2), instead of the default output. If both -v 1 and -v 2 are specified, produces output lines for all unpairable lines. OPERANDS
The pathnames of files to be used as input. If - (hyphen) is specified for either file, standard input is read. DESCRIPTION
The join field is the field in the input files that join looks at to determine what will be included in the output. One line appears in the output for each identical join field appearing in both file1 and file2. The output line consists of the join field, the rest of the line from file1, then the rest of the line from file2. Both input files must be sorted according to the collating sequence specified by the LC_COLLATE environment variable, if set, for the fields where they are to be joined (usually the first field in each line). Fields are normally separated by a space, a tab character, or a newline character. In this case, join treats consecutive separators as one, and discards leading separators. Use the -t option to specify another field separator. EXIT STATUS
The following exit values are returned: Successful completion. An error occurred. EXAMPLES
Note that the vertical alignment shown in these examples may not be consistent with your output. To perform a simple join operation on two files, phonedir and names, whose first fields are the same, enter: join phonedir names If phonedir contains the following telephone directory: Binst 555-6235 Dickerson 555-1842 Eisner 555-1234 Green 555-2240 Hrarii 555-0256 Janatha 555-7358 Lewis 555-3237 Takata 555-5341 Wozni 555-1234 and names is this listing of names and department numbers: Eisner Dept. 389 Frost Dept. 217 Green Dept. 311 Takata Dept. 454 Wozni Dept. 520 then join phonedir names displays: Eisner 555-1234 Dept. 389 Green 555-2240 Dept. 311 Takata 555-5341 Dept. 454 Wozni 555-1234 Dept. 520 Each line consists of the join field (the last name), followed by the rest of the line found in phonedir and the rest of the line in names. To display unmatched lines as well as matched lines, enter: join -a 2 phonedir names If phonedir contains: Binst 555-6235 Dickerson 555-1842 Eisner 555-1234 Green 555-2240 Hrarii 555-0256 Janatha 555-7358 Lewis 555-3237 Takata 555-5341 Wozni 555-1234 and names contains: Eisner Dept. 389 Frost Dept. 217 Green Dept. 311 Takata Dept. 454 Wozni Dept. 520 then join -a 2 phonedir names displays: Eisner 555-1234 Dept. 389 Frost Dept. 217 Green 555-2240 Dept. 311 Takata 555-5341 Dept. 454 Wozni 555-1234 Dept. 520 This performs the same join operation as in the first example, and also lists the lines of names that have no match in phonedir. It includes Frost's name and department number in the listing, although there is no entry for Frost in phonedir. To display selected fields, enter: join -o 2.3,2.1,1.2 phonedir names This displays the following fields: Field 3 of names (Department Number) Field 1 of names (Last Name) Field 2 of phonedir (Telephone Number) If phonedir contains: Binst 555-6235 Dickerson 555-1842 Eisner 555-1234 Green 555-2240 Hrarii 555-0256 Janatha 555-7358 Lewis 555-3237 Takata 555-5341 Wozni 555-1234 and names contains: Eisner Dept. 389 Frost Dept. 217 Green Dept. 311 Takata Dept. 454 Wozni Dept. 520 then join -o 2.3,2.1,1.2 phonedir names displays: 389 Eisner 555-1234 311 Green 555-2240 454 Takata 555-5341 520 Wozni 555-1234 To perform the join operation on a field other than the first, enter: sort -b -k 2,3 phonedir | join -1 2 - numbers This combines the lines in phonedir and names, comparing the second field of phonedir to the first field of numbers. First, this sorts phonedir by the second field because both files must be sorted by their join fields. The output of sort is then piped to join. The - (dash) by itself causes the join command to use this output as its first file. The -1 2 defines the second field of the sorted phonedir as the join field. This is compared to the first field of numbers because its join field is not speci- fied with a -2 option. If phonedir contains: Binst 555-6235 Dickerson 555-1842 Eisner 555-1234 Green 555-2240 Hrarii 555-0256 Janatha 555-7358 Lewis 555-3237 Takata 555-5341 Wozni 555-1234 and numbers contains: 555-0256 555-1234 555-5555 555-7358 then sort ... | join ... displays: 555-0256 Hrarii 555-1234 Eisner 555-1234 Wozni 555-7358 Janatha Each number in numbers is listed with the name listed in phonedir for that number. Note that join lists all the matches for a given field. In this case, join lists both Eisner and Wozni as having the telephone number 555-1234. The number 555-5555 is not listed because it does not appear in phonedir. ENVIRONMENT VARIABLES
The following environment variables affect the execution of join: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files). Determines the locale for the format and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: awk(1), cmp(1), comm(1), cut(1), diff(1), grep(1), paste(1), sdiff(1), sed(1), sort(1), uniq(1) Standards: standards(5) join(1)
All times are GMT -4. The time now is 01:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy