Looping and sed

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Looping and sed
# 1  
Old 08-18-2016
Looping and sed

Hi,

I've got text files (say file1 and file2) in a directory (say directory1) with several columns like this:

Code:
a b c d
a b c d

Code:
e f g h
e f g h

I need to add a header to the files in directory1 and the names of the columns must contain an identifier from another text file (say file3) like this:

Code:
12
34

So, the output should be like this:

Code:
12.X 12.Y 12.Z 12.T  
a b c d
a b c d

Code:
34.X 34.Y 34.Z 34.T  
e f g h
e f g h

In reality, file3 has over 6000 entries and there are more than 2 files that need a header, so I've written the following loop:

Code:
for a in $(ls directory1);
do
for i in $(cat file1);
do
sed '1 i\${i}.X ${i}.Y ${i}.Z ${i}.T \' ${a} > ${a}withheader;
done
done

However, the added headers look like this:

Code:
${i}.X ${i}.Y ${i}.Z ${i}.T

I suppose the '' prevent the shell to read the loop syntax. Is there anyway to avoid that? Or any other solution to add the headers?

Many thanks in advance!

---------- Post updated 08-18-16 at 01:20 AM ---------- Previous update was 08-17-16 at 11:32 PM ----------

I've figured it out, I just need to put '' around the looping variable:

Code:
sed '1 i\'${i}'.X '${i}'.Y '${i}'.Z '${i}'.T \' ${a} > ${a}withheader

Thank you!



Moderator's Comments:
Mod Comment Please use CODE (not HTML) tags as required by forum rules!

Last edited by rbatte1; 08-18-2016 at 06:38 AM.. Reason: Added CODE tags and changed ICODE tags for CODE tags
# 2  
Old 08-18-2016
So - just to get it right - for n > 2 files, you want to create m > 6000 new files each, resulting in n * m ( > 18000) files? Quite some opening and writing files ...
How about (untested, may need some polishing):
Code:
while read i
  do for a in directory1/*
        do echo $i.X $i.Y $i.z $i.T | cat - ${a} > ${a}withheader
        done
  done < file3

# 3  
Old 08-18-2016
As far as I have understood the task is more a merge:
for each file you need to read one line from the number file. Nested loops are not appropriate (and leads to inefficiency).
With shell-builtins
Code:
#!/bin/sh
for a in directory1/*
do
  read i <&3
  {
  echo "${i}.X ${i}.Y ${i}.Z ${i}.T"
  cat "$a"
  } > "${a}withheader"
done 3< file3

The file3 is opened with an extra descriptor 3 where the read reads one line from (could have been just the < and simple read here, but is safer in case some more code would read from stdin).
Like the for-done block gets an opened input file, there is an artificial { block } that gets an opened destination file as stdout.

Last edited by MadeInGermany; 08-18-2016 at 08:56 AM..
# 4  
Old 08-18-2016
@MadeinGermany: Wouldn't your interpretation (that I pondered on as well) mean that there need to be 6000 ( OK, OK - 6000 IS > 2!) input files as well?
# 5  
Old 08-18-2016
Yes indeed, if the number(ID) file is file3 with 6000 lines, this would mean (up to) 6000 input files.
However, the sample script in post#1 seems to take file1 as the number file...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping

Hey guys, so I am trying to do a loop script that will go through each folder (no gui so just each domain has a folder) and grab out the databases being used on that domain. I know I would use mysql -e "show databases where not 'information_schema';" once in each directory to pull the actual... (3 Replies)
Discussion started by: dough
3 Replies

2. Shell Programming and Scripting

Sed pattern space/looping conundrum

Although my sed skills are gradually developing, thanks in large part to this forum, I'm having a hard time dealing with pattern space and looping, which I suspect is what I'll need a better handle on to figure out my current issue, which is converting a multi line file like this: ... (4 Replies)
Discussion started by: tiggyboo
4 Replies

3. Shell Programming and Scripting

Looping

Hi, Now I have written a script which sorts the records in the file and splits them according to some condition. Now, I need to modify the script so that it reads all the files one after the other and does the sorting & splitting. Pls help me in reading all the files in a directory and... (8 Replies)
Discussion started by: Sunitha_edi82
8 Replies

4. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

5. Shell Programming and Scripting

help on looping using if/for or while

Hello, where can I get usefull information on the use of looping with for , if and while with extensive examples. Also use of variables in scripts (1 Reply)
Discussion started by: sam4now
1 Replies

6. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

7. Shell Programming and Scripting

Looping and using Sed

Hi guys I having som problem trying to use sed to get a file and insert inside another one. I'll try to explain better. I have a base.txt and using sed(having a array variables) I'm chaging some strings inside this file and saving as base1.txt. until here okay. Then, I have to get this... (4 Replies)
Discussion started by: digobh
4 Replies

8. Shell Programming and Scripting

looping

Hi I have around 100 users in sun server and have default home directory in /usr/home/<username> I want to clean their home directory time to time to make free space on root, as users generate many output files during usage of application. My idea is, generate a file with following command... (4 Replies)
Discussion started by: ishir
4 Replies

9. Shell Programming and Scripting

looping and awk/sed help

I am pretty new to this, but imagine what I am trying to do is possible iI am trying to make an automated DB comparison tool that selects all columns in all tables and compares them to the same thing in another DB. anyway I have created 2 files to help with this the first file is a... (13 Replies)
Discussion started by: Zelp
13 Replies

10. UNIX for Dummies Questions & Answers

Help with looping

Hi, Actually I have a file which consists data . for eg names. Then I want my sql query to read this file and produce the output. Currently I am using this FOR EG : FILENAME is NAMES for i in `cat NAMES` { sqlplus -s $CONNECTID << EOF spool rooh set heading off select... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question