Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Prepending information from filenames into files Post 302526923 by Scatterbrain26 on Thursday 2nd of June 2011 01:32:03 AM
Old 06-02-2011
Prepending information from filenames into files

I would like to know how to take information from a filename and place it into the text of the same file. Let's say I have a file called height_2_width_1.txt containing data that is related to a height of 2 and a width of 1, and the text originally looks like this:

Quote:
3 4
2 5
5 2
7 5
where these two columns are two other variables (depth and brightness, or whatever). Basically, I would like to add two columns to this file, based on the height and width of the first file, such that the end result would look like this:

Quote:
2 1 3 4
2 1 2 5
2 1 5 2
2 1 7 5
So, if I did this, I would have the height 2 and width 1 information put in columns next to the other data related to these particular heights and weights. So a case with a depth of 3 and brightness of 4 would have the height of 2 and width of 1 prepended to it.

And then I have multiple files named height_X_width_Y.txt, such that I have an array for height and width (let's say 1,2,3,4 for each X and Y variable, so I have the following files: height_1_width_1.txt, height_1_width_2.txt, etc). I would like to create a perl script that does the same thing for all of the files. I'm just not sure what the best way would be to do this would be. I hope this made sense. Smilie

This is my attempt thus far, but I haven't been able to get it to work:

Code:
#!/usr/bin/perl -w

my @height=(1,2,3,4);

my @width=(1,2,3,4);

foreach my $height (@height)
{
    foreach my $width (@width)
    {
		$filename = "height_${height}_width_${width}.txt";
		system("sed -i "" '1,4 ${height} ${width}' $filename");
	}
}


Last edited by pludi; 06-02-2011 at 09:55 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies

2. Shell Programming and Scripting

Getting a list of filenames of moved files

I'm moving a list of files of some extension and I wish to output the moved filenames into a text file, I tried using the command below, but after all the files are moved, I got a blank file. find /abc/temp -type f -mtime +365 \( -name "*.bak" -o -name "*.log" \) -exec mv -f {} /junk \; >>... (3 Replies)
Discussion started by: chengwei
3 Replies

3. Shell Programming and Scripting

copy all files with the same filenames as those in another folder

Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. All image files in folder A are able to find their corresponding description files in folder B. That is to say,... (3 Replies)
Discussion started by: jiapei100
3 Replies

4. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

5. AIX

How to grep filenames from a file in other files?

Hi, We have a file called applcust.txt where we keep records of our customizations in the following format. We want to verify whether the file, which is in 3rd column in this file is there in another file called patch999999.log to find out if any of our customization has got overwritten by the... (2 Replies)
Discussion started by: sshah1001
2 Replies

6. Shell Programming and Scripting

Redirection after prepending timestamp

Hi all, I have little experience with Scripting so hoping someone may be able to help me or point me in the right direction. I have a shell script which was outputting uncaught exceptions to a log file. $JAVA_MAIN_CLASS > $LOGNAME 2>&1 What I want to be able to do is prepend a timestamp on... (8 Replies)
Discussion started by: MacAonghusa
8 Replies

7. Shell Programming and Scripting

Sorting a list of filenames but keeping the path information.

Hi All I've googled around for this and can't see a way of doing it. I have a file that contains a number of records that are layed out something like the following. /path/to/directory/that/contains/a/file/I/need/filename.pdf The path itself can vary both in terms of the names and the... (7 Replies)
Discussion started by: Bashingaway
7 Replies

8. UNIX for Dummies Questions & Answers

Prepending lines with file name

I have a number of dat-files, such as abc.dat, def.dat etc, as follows: 2011-07-01 100.0 2011-07-02 101.0 2011-07-03 101.7 I want to prepend the file with the base file name; so for abc.dat it would result in the following: abc 2011-07-01 100.0 abc 2011-07-02 101.0 abc 2011-07-03 ... (5 Replies)
Discussion started by: figaro
5 Replies

9. Shell Programming and Scripting

Sed script for appending & prepending

Hello Mates I am trying to write a script, which appends and prepends the text in a file. I tried testing with a small file and it worked fine. but for the large file, the script is wiping the entire file and adds only the word to be appended in the file. mv $file_name $file_name.bak sed... (6 Replies)
Discussion started by: sathyaac
6 Replies

10. Shell Programming and Scripting

Renaming files with Spaces in Filenames

Entry level scripter. Any help appreciated. for file in *; do rename '4321_' '' $file ; done Doesn't work for files with spaces in between FOr eg 4321_1004.dat is renamed to 1004.dat but 4321_1004 2008.dat stays the same (1 Reply)
Discussion started by: davnavin
1 Replies
GLVIEWPORT(3G)							    OpenGL 3.3							    GLVIEWPORT(3G)

NAME
glViewport - set the viewport C SPECIFICATION
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height); PARAMETERS
x, y Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0). width, height Specify the width and height of the viewport. When a GL context is first attached to a window, width and height are set to the dimensions of that window. DESCRIPTION
glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. Let x nd y nd be normalized device coordinates. Then the window coordinates x w y w are computed as follows: x w = x nd + 1 width 2 + x y w = y nd + 1 height 2 + y Viewport width and height are silently clamped to a range that depends on the implementation. To query this range, call glGet() with argument GL_MAX_VIEWPORT_DIMS. ERRORS
GL_INVALID_VALUE is generated if either width or height is negative. ASSOCIATED GETS
glGet() with argument GL_VIEWPORT glGet() with argument GL_MAX_VIEWPORT_DIMS SEE ALSO
glDepthRange() COPYRIGHT
Copyright (C) 1991-2006 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/. OpenGL 3.3 03/08/2011 GLVIEWPORT(3G)
All times are GMT -4. The time now is 12:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy