split row into lines and insert file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split row into lines and insert file name
# 1  
Old 07-19-2011
split row into lines and insert file name

I have a directory with several hundred files.
The file format is a space delimited row with an unknown number of columns:
A B C D E F G ...

I need to turn this format

File1 A
File1 B
File2 A
File3 A
File3 B
File3 C
...

I can use grep to display the filename next to each row of results, but i have not found a way to split the row into new lines while retaining the file names.
# 2  
Old 07-19-2011
This small awk script will do what you need:

Code:
awk '{
    for( i = 1; i <= NF; i++ )
        printf( "%s %s\n", FILENAME, $(i) );
    } ' file1 file2 file3

If you have lots of files, listing them on the command line might not be possible (too many/too large). If you want to run it on all files in the current directory this will work:

Code:
ls | xargs  awk '{
    for( i = 1; i <= NF; i++ )
        printf( "%s %s\n", FILENAME, $(i) );
    } '

These should get you started.
# 3  
Old 07-20-2011
Thanks! Got what i needed out of the second script!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

Split a file by start and end row.

I have a file which looks something as following, I would like to split to several files, The start and end of each file is 'FILE' and end with 'ASCII... ' . At the same time for each file in the first column add 100 and also second column add 100 the rest of the column as it is , see example of... (2 Replies)
Discussion started by: tk2000
2 Replies

3. UNIX for Dummies Questions & Answers

Insert row into empty file...how?

Greetings: I generate an empty flat file just fine when there's no data returned from my process, as the customer wants one always (using the 1st line of the below script). However, they also want at least the column names in this flat file (row 1, the only row to be in the emply file). I'm... (7 Replies)
Discussion started by: Benrosa
7 Replies

4. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

5. Shell Programming and Scripting

Split file - Include first and last row in each file.

Hi All, We have file structure as following. (Coma ';' delimiter file) Header 1;... 1;... 1;... 2;... 2;... 2;... Trailer We need to split this file as following. (for each change in first field, after header, we need to create new file with Header and Trailer line) Header... (21 Replies)
Discussion started by: meetmedude
21 Replies

6. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

7. Shell Programming and Scripting

insert multiple lines into a file

Hi all, I've got some problems with editing a big configuration file .. its about 2k lines long.. anyway what I need is to place certain text to certain line number.. lets say I need to place "Something" on line 980 .. "something" else on line number 1500 and so on without tempering the rest of... (12 Replies)
Discussion started by: c0mrade
12 Replies

8. Shell Programming and Scripting

Split a record ( in a row) and make it as new row

Hi All, The following is the scenario id name -------------- 1 William;Johnson 2 Azim;Abdul 3 Grasim . . etc.... I need the following output id name -------------- 1 William 1 Johnson 2 Azim (2 Replies)
Discussion started by: kottursamy
2 Replies

9. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question