Replacing first line of file by >filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing first line of file by >filename
# 15  
Old 04-08-2011
Here is a method to process the files one after another.

Sample Files:
-> ls S5*
S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03

Contents of the files:
==> cat S5*
>SK1.chr01
blah
blah
blah
>SK1.chr02
blah
blah
blah
>SK1.chr03
blah
blah
blah

Write the code:
==> vi modfiles.ksh

Code:
#!/bin/ksh
### Loop through the files you intend to modify
for origfile in $(ls -1 S5*)
do
   ### Rename the file temporarily
   mv ${origfile} ${origfile}.tmp

   ### Create brand new origfile with... ">filename" on the first line
   echo "${origfile}" | awk '{printf(">%s\n", $1)}' > ${origfile}

   ### Append lines 2 to the end to the new filee
   cat ${origfile}.tmp | tail +2 >> ${origfile}

   ### Clean up work file
   rm ${origfile}.tmp
done

Run it:
==> ./modfiles.ksh

Verify the contents:
-> cat S5*
>S5_SK1.chr01
blah
blah
blah
>S5_SK1.chr02
blah
blah
blah
>S5_SK1.chr03
blah
blah
blah
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

2. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

3. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

4. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

5. Shell Programming and Scripting

Adding filename to each line of the file

Hi, I am a relative new bee in scripting. I need to develop a script such that the code would iterate through each file in a source directory and append every line of the file with '|' and the corresponding file filename. eg INPUT file IF927_1.dat - H|abc... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

6. UNIX for Dummies Questions & Answers

Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can... (5 Replies)
Discussion started by: s.plumb
5 Replies

7. Shell Programming and Scripting

Replacing the last character for each line in a file

Hello, I have a csv file and will like to replace the last character of each line in the file with Z (20 Replies)
Discussion started by: 123script
20 Replies

8. UNIX for Dummies Questions & Answers

Replacing string with filename

Hi All, I've recently run a script that inserts the filename into all files of my active directory. Now I want to move the filename string and have it replace text a few lines down. In other words, here's what I'm trying to do. Here is a file called 'goodtimes': " goodtimes Hi, Welcome... (1 Reply)
Discussion started by: calrog
1 Replies

9. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question
ppmtosixel(1)						      General Commands Manual						     ppmtosixel(1)

NAME
ppmtosixel - convert a portable pixmap into DEC sixel format SYNOPSIS
ppmtosixel [-raw] [-margin] [ppmfile] DESCRIPTION
Reads a portable pixmap as input. Produces sixel commands (SIX) as output. The output is formatted for color printing, e.g. for a DEC LJ250 color inkjet printer. If RGB values from the PPM file do not have maxval=100, the RGB values are rescaled. A printer control header and a color assignment table begin the SIX file. Image data is written in a compressed format by default. A printer control footer ends the image file. OPTIONS
-raw If specified, each pixel will be explicitly described in the image file. If -raw is not specified, output will default to com- pressed format in which identical adjacent pixels are replaced by "repeat pixel" commands. A raw file is often an order of magni- tude larger than a compressed file and prints much slower. -margin If -margin is not specified, the image will be start at the left margin (of the window, paper, or whatever). If -margin is speci- fied, a 1.5 inch left margin will offset the image. PRINTING
Generally, sixel files must reach the printer unfiltered. Use the lpr -x option or cat filename > /dev/tty0?. BUGS
Upon rescaling, truncation of the least significant bits of RGB values may result in poor color conversion. If the original PPM maxval was greater than 100, rescaling also reduces the image depth. While the actual RGB values from the ppm file are more or less retained, the color palette of the LJ250 may not match the colors on your screen. This seems to be a printer limitation. SEE ALSO
ppm(5) AUTHOR
Copyright (C) 1991 by Rick Vinci. 26 April 1991 ppmtosixel(1)