Visit Our UNIX and Linux User Community


Read and concatenate content file and file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and concatenate content file and file name
# 1  
Old 09-02-2016
Hammer & Screwdriver Read and concatenate content file and file name

Hi all,
i need a bash script.
I have a 3 file named Milano, Torino, Firenze
Into file i have:

Milano
Code:
Marco
Luca
Giorgio
Michele
Patrizio

Torino
Code:
Marco
Giulio
Emilio
Michele

Firenze
Code:
Luca
Giorgio
Marco
Saverio
Emilio

The output should be a all_city.csv file like:
Code:
User;Milano;Torino;Firenze
Marco;assigned;assigned;assigned
Luca;assigned;;assigned
Giorgio;assigned;;assigned
Michele;assigned;assigned;
Patrizio;assigned;;
Giulio;;assigned;
Emilio;;assigned;assigned
Saverio;;;assigned

Code:
$ echo $BASH_VERSION
4.1.2(1)-release

Any ideas? I try whit array, cut, join, etc.. but with no solution Smilie
Thanks to all!!

Last edited by kamose; 09-02-2016 at 01:41 PM.. Reason: update
# 2  
Old 09-02-2016
something along these lines....
awk -f kam,awk Milano Torino Firenze where kam.awk is:
Code:
BEGIN {
  OFS=";"
}
FNR==1 {cityA[++cityN]=FILENAME }
{ userC[$1,FILENAME];userA[$1] }

END {
  printf("%s", "User" OFS)
  for(c=1; c<=cityN; c++)
   printf("%s%s",cityA[c], (c+1>cityN)?ORS:OFS)

  for(u in userA) {
     printf("%s", u OFS)
     for(c=1; c<=cityN; c++)
       printf("%s%s", ( (u,cityA[c]) in userC)?"assigned":"", (c+1>cityN)?ORS:OFS)
  }
}

# 3  
Old 09-02-2016
If you save the script vgersh99 suggested in a file named kam.awk, you need to use the same name in the command line when you invoke awk:
Code:
awk -f kam.awk Milano Torino Firenze

I assume the <comma> instead of <period> in the filename in the command line was a typo.

With vgersh99's script the order of cities in the output is in the same order as the input files, but the order of users in the output is random and may vary depending on which version of awk runs your script. If you want to guarantee that the order of users in the output matches the order in which user names were first seen in input files, you could try this slightly more complicated script. If you save the following in a file named merger:
Code:
#!/bin/ksh
awk '
BEGIN {	# Set output field separator.
	OFS = ";"
}
FNR == 1 {
	# Gather city names from filenames.
	city[++ncity] = FILENAME
}
{	# Gather data from the current input file.
	# Have we seen this user beofore?
	if(!($1 in user)) {
		# No.  Add this user to the list of know users...
		user[$1]
		# and keep track of the order in which users were found.
		order[++nuser] = $1
	}
	# Note that we have seen this user in this city.
	assigned[$1, ncity]
}
END {	# Print header...
	printf("User%s", OFS)
	for(i = 1; i <= ncity; i++)
		printf("%s%s", city[i], (i == ncity) ? ORS : OFS)

	# Print data for each user...
	for(i = 1; i <= nuser; i++) {
		# print user name...
		printf("%s%s", order[i], OFS)
		for(j = 1; j <= ncity; j++)
			# and print assigned data for each city.
			printf("%s%s",
			    ((order[i], j) in assigned) ? "assigned" : "",
			    (j == ncity) ? ORS : OFS)
	}
}' "$@"

and make it executable:
Code:
chmod +x merger

and invoke it with:
Code:
./merger Milano Torino Firenze > all_city.csv

it will write:
Code:
User;Milano;Torino;Firenze
Marco;assigned;assigned;assigned
Luca;assigned;;assigned
Giorgio;assigned;;assigned
Michele;assigned;assigned;
Patrizio;assigned;;
Giulio;;assigned;
Emilio;;assigned;assigned
Saver;;;assigned

into all_city.csv exactly as requested.

This was written and tested with a Korn shell, but will work with any shell that uses Bourne shell syntax (including bash and several others). If you want to run this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-05-2016
Linux

Don Cragun you rulessss, but also thanks to vgersh99.

God bless you Smilie

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to create concatenate the shell variable with file content

Hi Guys, I have a file. Each record needs to inserted into a table. The table also have other columns which needs to be inserted with Shell variables. The following is the file. Error code None. Error Code 1 The shell script is having these variables. Name=Magesh Dep=Coding ... (1 Reply)
Discussion started by: mac4rfree
1 Replies

2. Shell Programming and Scripting

While read line ignores the '\' in file content

I need to read temp.$i file content line by line through while loop but somehow the '\' do not appear in output.. Can someone guide how to read this exact content line by line in unix : if then cat temp.$i | head -1 # the file content appears fine while... (13 Replies)
Discussion started by: Prev
13 Replies

3. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

4. Shell Programming and Scripting

Need help to read rows from one file and concatenate to another

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (6 Replies)
Discussion started by: carlos_anubis
6 Replies

5. UNIX for Dummies Questions & Answers

Read rows from source file and concatenate output

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (0 Replies)
Discussion started by: carlos_anubis
0 Replies

6. Shell Programming and Scripting

How to read file and only output certain content

Hi - I have a file containing data like :- cn=tommy,cn=users,c=uk passwordexpirydate=20100530130623z cn=jane,cn=users,c=uk passwordexpirydate=20100423140734z cn=michael,cn=users,c=uk passwordexpirydate=20100331020044z I want to end up with a file that looks like:-... (6 Replies)
Discussion started by: sniper57
6 Replies

7. Shell Programming and Scripting

read file content

i have one file abhi.txt its contents are home8/mc09ats/UnixCw/backup/file1 home8/mc09ats/file2 i want to read this content of file using while loop.. in this i want to seperate the content as follows path=home8/mc09ats/UnixCw/backup file=file1 echo path echo file can you... (1 Reply)
Discussion started by: AbhijitIT
1 Replies

8. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

9. Shell Programming and Scripting

read a file and use the content for mapping

help me pls.. :( i want to read a mapping file. Below is the content of my mapping file. 6221,189,SMSC1,OMC1,WAP1 6223,188,SMSC2,OMC2,WAP2 so when my program running msisdn="622130302310" while not EOF if substring($msisdn,1,4) == "6221" -- > "6221" read from the file then echo... (0 Replies)
Discussion started by: voidmain
0 Replies

10. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

Featured Tech Videos