Sponsored Content
Top Forums Shell Programming and Scripting Read and edit multiple files using a while loop Post 302448830 by felipe.vinturin on Friday 27th of August 2010 06:23:14 AM
Old 08-27-2010
Ok!

We have two options:

1. As zaxxon commented, if your sed supports the "-i" option, in place change, use it.
Code:
sed -i -e '1,$s/old string/new string/g' myfirstfileinloop.txt

2. If it does not, you must redirect the output, like the following:
Code:
ls -1 | while read fname
do
      myFileTmp=${fname}.tmp
      sed -e '1,$s/old string/new string/g' "${fname}" > "${myFileTmp}"
      # If you want to remove the original file uncomment the line below
      [ $? -eq 0 ] && rm -f "${fname}"
done

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Edit Multiple Files in VI

Here's what I have... $ vi foo1 - open foo1 and work around for a while. I yank a few lines into a buffer and then :w to save. Next I :e foo2 to open foo2 and paste my buffer. I :w to save, but I would like to then be able to go directly back into foo1 where I was before I opened foo2. ... (4 Replies)
Discussion started by: djschmitt
4 Replies

2. Shell Programming and Scripting

help with a 'while read' loop to change the names of files

Hi, I am new to scripting, so any help on this would be much appreciated. I am trying to rename a bunch of files, taking the names sequentially from a list read in another file... # ls oldnames file_1 file_2 file_3 # cat names red yellow green I want the files to take on the... (6 Replies)
Discussion started by: starsky
6 Replies

3. Shell Programming and Scripting

Read multiple arguments in for loop each time

Hi, Guys I am new to shell programming and just get stuck with one simple question. please kindly help. According to the tutorial here, we can do something like for NODE in "ABC 10" "EFG 20" do set -- $NODE echo "letter is $1, number is $2" done And the result will... (3 Replies)
Discussion started by: yuanli
3 Replies

4. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

5. Shell Programming and Scripting

loop to read multiple username and password

Hello all, I am i am trying to read username password. Bassicaly, i have file called sidlist and it has my database name, username and password.... looks something like this.... db1, user1, pass1 db2, user2, pass2 db3, user3, pass4 but i dont know how to make it work, until i get... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

6. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

7. Shell Programming and Scripting

Read loop from two files using descriptors

What I would like to do is read each line in the atdinfile: A sample atdinfile would look like this: 651 652 653 654 655 656 657 658 659 660 661 664 665 666 667 668 (5 Replies)
Discussion started by: woodson2
5 Replies

8. Shell Programming and Scripting

Help require to edit multiple files

I have 6 different pipe delimiter files. My loads failing due to missing company code. File1: 31 st field is company code. 402660076310|2014-12-10 17:22:39|2280361|MRYKI|1||CA|92507|US||1|1|0|0|0||N|A1|ONT|1001891771660009250700402660076310|WM|0201|RALA |2014-12-12|5|2014-12-12|5||FRI - 12... (4 Replies)
Discussion started by: srikanth38
4 Replies

9. Programming

Read .xlsx and text files in a loop using openpyxl

I have a list of .xlsx files (names 1.xlsx, 2.xlsx etc) in a directory, on which I need to insert data from its corresponding text file (named 1.txt, 2.txt etc) on the second worksheet named 'Filtered' and save it. The code I am trying is #!/usr/bin/python import os from... (8 Replies)
Discussion started by: nans
8 Replies

10. Shell Programming and Scripting

Using sed to edit multiple files

Created a shell script to invoke sed to edit multiple files, but am missing something. Here's the shell script: oracle:$ cat edit_scripts.sh #!/bin/sh #------------------------------------------------------------------------------ # edit_scripts.sh # # This script executes sed to make global... (4 Replies)
Discussion started by: edstevens
4 Replies
YAP(1)							      General Commands Manual							    YAP(1)

NAME
yap, more - yet another pager SYNOPSIS
yap [ -cnuq ] [ -n ] [ +command ] [ name ... ] DESCRIPTION
Yap is a program allowing the user to examine a continuous text one screenful at a time on a video display terminal. It does so by pausing after each screenful, waiting for the user to type a command. The commands are enumerated later. Yap's main feature is, that it can page both forwards and backwards, even when reading from standard input. The command line options are: -n An integer which is the size (in lines) of a page (the initial page-size. -c Normally, yap will display each page by beginning at the top of the screen and erasing each line just before it displays on it. If your terminal cannot erase a line, yap will clear the screen before it displays a page. This avoids scrolling the screen, making it easier to read while yap is writing. The -c option causes yap to scroll the screen instead of beginning at the top of the screen. This is also done if your terminal cannot either erase a line or clear the screen. -u Normally, yap handles underlining such as produced by nroff in a manner appropriate to the particular terminal: if the terminal can perform underlining well (t.i., the escape sequences for underlining do not occupy space on the screen), yap will underline under- lined information in the input. The -u option supresses this underlining. -n Normally, yap also recognises escape sequences for stand-out mode or underlining mode in the input, and knows how much space these escape sequences will occupy on the screen, so that yap will not fold lines erroneously. The -n option supresses this pattern matching. -q This option will cause yap to exit only on the "quit" command. +command command is taken to be an initial command to yap. Yap uses the termcap(5) database to determine the terminal capabilities and the default page-size. It examines the $TERM environment vari- able to identify the terminal type. If $TERM is not set, it defaults to dumb. Yap Also examines the $TERMCAP environment variable to locate the termcap(5) database. If $TERMCAP is not set, it defaults to /etc/termcap. Yap looks in the $YAP environment variable to pre-set flags. For instance, if you prefer the -c mode of operation, just set the $YAP envi- ronment variable to -c. The commands of yap can be bound to sequences of keystrokes. The environment variable $YAPKEYS may contain the bindings in the form of a list of colon-separated `name=sequence' pairs. The name is a short mnemonic for the command, the sequence is the sequence of keystrokes to be typed to invoke the command. This sequence may contain a ^X escape, which means control-X, and a X escape, which means X. The latter can be used to get the characters `^', `' and `:' in the sequence. There are two keymaps available, the default one and a user-defined one. You can switch between one and the other with the change keymap command. The yap commands are described below. The mnemonics for the commands are given in parentheses. The default key sequences (if any) are given after the mnemonic. Every command takes an optional integer argument, which may be typed before the command. Some commands just ignore it. The integer argument is referred to as i. Usually, if i is not given, it defaults to 1. visit previous file (bf) P Visit the i-th previous file given in the command line. scroll one line up or go to line (bl) ^K or k If i is not given, scroll one line up. Otherwise, i will be interpreted as a line number. A page starting with the line indicated will then be displayed. bottom (bot) l or $ Go to the last line of the input. display previous page (bp) - Display the previous page, consisting of i lines, (or page-size lines if no argument is given). display previous page and set pagesize (bps) Z Display the previous page, consisting of i lines, (or page-size lines if no argument is given). If i is given, the page-size is set to i. scroll up (bs) ^B Scroll up i lines (or scroll-size lines if i is not given. Initially, the scroll-size is 11). search backwards for pattern (bse) ? Search backwards for the i-th occurrence of a regular expression which will be prompted for. If there are less than i occurrences of the expression, the position in the file remains unchanged. Otherwise, a page is displayed, starting two lines before the place where the expression was found. The user's erase and kill characters may be used to edit the expression. Erasing back past the first character cancels the search command. Note: Some systems do not have regex(3). On those systems, searches are still supported, but regular expressions are not. skip lines backwards (bsl) S Skip i lines backwards and display a page. skip pages backwards (bsp) F Skip i pages backwards and display a page. scroll up and set scrollsize (bss) b Scroll up i lines (or scroll-size lines if i is not given. If i is given, the scroll-size is set to i. change key map (chm) ^[ Change from the current key map to the other (if there is one). exchange current page and mark (exg) x Set the mark to the current page, and display the previously marked page. visit next file (ff) N Visit the i-th next file given in the command line. scroll one line down or go to line (fl) ^J or ^M or j If i is not given, scroll one line down. Otherwise, i will be interpreted as a line number. A page starting with the line indicated will then be displayed. display next page (fp) <space> Display the next page, consisting of i lines, (or page-size lines if no argument is given). display next page and set pagesize (fps) z Display the next page, consisting of i lines, (or page-size lines if no argument is given). If i is given, the page-size is set to i. scroll down (fs) ^D Scroll down i lines (or scroll-size lines if no argument is given). search forwards for pattern (fse) / Search forwards for the i-th occurrence of a regular expression which will be prompted for. If there are less than i occurrences of the expression, the position in the file remains unchanged. Otherwise, a page is displayed, starting two lines before the place where the expression was found. The user's erase and kill characters may be used to edit the expression. Erasing back past the first character cancels the search command. Note: Some systems do not have regex(3). On those systems, searches are still supported, but regular expressions are not. skip lines forwards (fsl) s Skip i lines and display a page. skip pages forwards (fsp) f Skip i pages and display a page. scroll down and set scrollsize (fss) d Scroll down i lines (or scroll-size lines if i is not given. If i is given, the scroll-size is set to i. help (hlp) h Give a short description of all commands that are bound to a key sequence. set a mark (mar) m Set a mark on the current page. repeat last search (nse) n Search for the i-th occurrence of the last regular expression entered, in the direction of the last search. repeat last search in other direction (nsr) r Search for the i-th occurrence of the last regular expression entered, but in the other direction. quit (qui) Q or q Exit from yap. redraw (red) ^L Redraw the current page. repeat (rep) . Repeat the last command. This does not always make sense, so not all commands can be repeated. shell escape (shl) ! Invoke the shell with a command that will be prompted for. In the command, the characters `%' and `!' are replaced with the current file name and the previous shell command respectively. The sequences `\%' and `!' are replaced by `%' and `!' respectively. The user's erase and kill characters can be used to edit the command. Erasing back past the first character cancels the command. pipe to shell command (pip) | Pipe the current input file into a shell command that will be prompted for. The comments given in the description of the shell escape command apply here too. go to mark (tom) ' Display the marked page. top (top) ^^ Display a page starting with the first line of the input. visit file (vis) e Visit a new file. The filename will be prompted for. If you just type a return, the current file is revisited. write input to a file (wrf) w Write the input to a file, whose name will be prompted for. The commands take effect immediately, i.e., it is not necessary to type a carriage return. Up to the time when the command sequence itself is given, the user may give an interrupt to cancel the command being formed. AUTHOR
Ceriel J.H. Jacobs FILES
/etc/termcap Terminal capabilities data base SEE ALSO
termcap(5), regex(3). BUGS
Yap will find your terminal very stupid and act like it, if it has no way of placing the cursor on the home position, or cannot either erase a line or insert one. In lines longer than about 2000 characters, a linefeed is silently inserted. The percentage, given in the prompt when yap reads from a file (and knows it), is not always very accurate. local YAP(1)
All times are GMT -4. The time now is 02:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy