Visit Our UNIX and Linux User Community


Adding characters at the top of all files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding characters at the top of all files in directory
# 1  
Old 10-06-2009
Adding characters at the top of all files in directory

Hi Unix experts;

I have 30000 files in a directory and am willing to do the following changes on each of them. The input files look like the following:
Code:
1 ,  2
3  ,  4
5  ,  6
7  ,  8
9  ,  10

the output will have # in top 10 lines, insert space instead of comma. This looks like:

Code:
#
#
#
#
#
#
#
#
#
#
1     2
3     4
5     6
7     8
9    10

I have come up with the following. But don't know how I can put # in the first 10 lines:

for file in $(ls -l *)
do
echo $file | sed 's/,/ /g' >output
done

Regards
# 2  
Old 10-06-2009
What about:

bash code:
  1. ls | while read FILE; do
  2. cat << ! > $FILE.new
  3. #
  4. #
  5. #
  6. #
  7. #
  8. #
  9. #
  10. #
  11. #
  12. #
  13. $(sed "s/,/ /" $FILE)
  14. !
  15. cp -f $FILE.new $FILE && rm $FILE.new
  16. done

Or to avoid a UUOC award...
bash code:
  1. ls | while read FILE; do
  2. echo "#
  3. #
  4. #
  5. #
  6. #
  7. #
  8. #
  9. #
  10. #
  11. #
  12. $(sed "s/,/ /" $FILE)
  13. " > $FILE.new
  14. cp -f $FILE.new $FILE && rm $FILE.new
  15. done

Last edited by Scott; 10-06-2009 at 08:47 PM.. Reason: not sure this new highlight=bash thing is working. makes cutting and pasting a real pain
# 3  
Old 10-06-2009
Quote:
Originally Posted by scottn
What about:

bash code:
  1. ls | while read FILE; do
  2. cat << ! > $FILE.new
  3. #
  4. #
  5. #
  6. #
  7. #
  8. #
  9. #
  10. #
  11. #
  12. #
  13. $(sed "s/,/ /" $FILE)
  14. !
  15. cp -f $FILE.new $FILE && rm $FILE.new
  16. done

Or to avoid a UUOC award...
bash code:
  1. ls | while read FILE; do
  2. echo "#
  3. #
  4. #
  5. #
  6. #
  7. #
  8. #
  9. #
  10. #
  11. #
  12. $(sed "s/,/ /" $FILE)
  13. " > $FILE.new
  14. cp -f $FILE.new $FILE && rm $FILE.new
  15. done

That merits a UUOL award. (And it will fail if there are any spaces in any of the filenames.)

Code:
for file in *
do
  {
    printf "%s\n" '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'
    sed 's/,/ /g' "$file"
  } > output && mv output "$file"
done

# 4  
Old 10-06-2009
Thanks for different options! How can i add one space at the beginning of each line while doing the previous task, i.e.
Code:
#
#
#
 1    2
 3    4
 5    6
 6    7
 8    9

The only change is the space befor each data line only

---------- Post updated at 08:15 PM ---------- Previous update was at 08:01 PM ----------

whoops!! I did it by adding another sed!

Thank you very much anyways

Last edited by nxp; 10-06-2009 at 10:03 PM.. Reason: editing
# 5  
Old 10-06-2009
Quote:
Originally Posted by nxp
whoops!! I did it by adding another sed!

You don't need another sed; just another command to the same sed:

Code:
for file in *
do
  {
    printf "%s\n" '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'
    sed -e 's/,/ /g' -e 's/^/ /' "$file"
  } > output && mv output "$file"
done

'Nuff sed!

Previous Thread | Next Thread
Test Your Knowledge in Computers #371
Difficulty: Medium
PHP opcodes are generated each time the program is launched and are always interpreted and not just-in-time compiled.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add filenames to top of each files in a directory

Hello, I tried different solutions given in various linux portals but no luck.. The directory consists of files with no-extension. Each file has only one line. I need to add each filename to the top of each file so eventually each file will have two rows. Filenames have spaces between words... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Request for advise on how to remove control characters in a UNIX file extracted from top command

Hi, Please excuse for posting new thread on control characters, I am facing some difficulties in removing the control character from a file extracted from top command, i am able to see control characters using more command and in vi mode, through cat control characters are not visible ... (8 Replies)
Discussion started by: karthikram
8 Replies

3. UNIX for Dummies Questions & Answers

Display Directory path on top

Hello, I am using below in my .bashrc to dynamically display user/host/directory information whenever i logged in to my Red hat Linux 64bit OS, BASH shell. export PS1='$PWD>' output: /dbms/app/oracle/product/11.2.0/dbhome_1> But i want to display absolute path information to be displayed... (1 Reply)
Discussion started by: Ariean
1 Replies

4. Shell Programming and Scripting

How to get top level parent directory

Hi All, I have a directory like this: /u01/app/oracle/11gSE1/11gR203 How do i get the top level directory /u01 from this? Tried dirname and basename but dint help. I can this using echo $ORACLE_HOME | awk -F"/" '{print "/"$2}'. But I am trying to find out if there is a better way of doing it... (4 Replies)
Discussion started by: nilayasundar
4 Replies

5. Shell Programming and Scripting

Adding a line to 1000's of files right after x amt of characters.

I am trying to add a single line of text to every file in a particular folder. There are thousands of files in the folder. Each file contains this same start of the first line: {d "%w- %d %m-, %Y - %T"} <some message here> with the rest of the text following the second curly bracket... (10 Replies)
Discussion started by: dlundwall
10 Replies

6. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

7. Shell Programming and Scripting

Adding additional characters while preserving original text

Hi Forum. I'm struggling on this relatively easy request to add additional 4 0's to an existing text in a file (whenever I see the pattern -# where # represents any number) using sed command while preserving the rest of the text in the files. Original Text: $DBConnection_EDW=SAS2EDW... (5 Replies)
Discussion started by: pchang
5 Replies

8. Shell Programming and Scripting

Adding Characters to a Word List

If I had a word list with a large amount of words in it, how would I (using a unix command) add, say, 123 to the end of each word? EDIT: The word list is stored in a large text file. I need a command that applies the ending to each word in the file and saves the result in a new text file. (7 Replies)
Discussion started by: evillion
7 Replies

9. Shell Programming and Scripting

AWK script to omit top characters with dashes

Hi I have an AWK script that takes an input file and outputs it as CSV format. The problem is its also outputting the characters at the top which are dashes(-------) and i want it to leave them out. My script is as follows. BEGIN { count=1; } /^/ { count+=1 if ( count > 2 ){... (3 Replies)
Discussion started by: magikminox
3 Replies

10. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies

Featured Tech Videos