Sponsored Content
Top Forums Shell Programming and Scripting Script to process a list of items and uncomment lines with that item in a second file Post 303042875 by MadeInGermany on Thursday 9th of January 2020 04:51:46 AM
Old 01-09-2020
Or do all in bash - here is a well documented bash version:
Code:
#! /bin/bash
# bash v3 or higher

# file with list of items to find in modify_file
list_file=$1
# file for which a modified copy will be created
modify_file=$2
# final output file
new_file='new_'$modify_file

echo "reading values from '$list_file'"
# clear (and implicityly declare) an array
list=()
while read line
do
  echo "adding '$line' to list"
  list+=("$line")
done < $list_file

echo "reading '$modify_file'"
# loop through all the lines in the list_file
while IFS= read -r line <&3
do
   # look for the value in the current line
   # if the value is found on a line, print the substring of $0 skipping the first character
   # otherwise just print the line

   echo "processing line $((++linecnt))"
    # preset the new line
   outline=$line
   # loop through the array values
   for i in "${list[@]}"
   do
      look_for="'$i'"
      # =~ is ERE match, == is glob match
      # $look_for in quotes --> do not interpret special characters in it
      #if [[ $line =~ "$look_for" ]]
      if [[ $line == *"$look_for"* ]]
      then
         outline=${line:1}
         # further matches will do the same, so we can break the loop here
         break
      fi
   done
   # print the new line
   printf "%s\n" "$outline" >&4
done 3< "$modify_file" 4> "$new_file"


Last edited by MadeInGermany; 01-09-2020 at 06:09 AM.. Reason: break added, cosmetic changes
This User Gave Thanks to MadeInGermany For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

retrieve what the currently selected item is in a dropdown select list using perl tk

I have a dropdown menu built in perl tk (I am using active state perl). I want to select a value from the dropdown menu and I want to be able to perform some other actions depending upon what value is selected. I have all the graphical part made but I dont know how to get the selected value. Any... (0 Replies)
Discussion started by: lassimanji
0 Replies

2. Shell Programming and Scripting

comment and uncomment a line with Shell Script

Requirement is: 1. comment and uncomment the line with Shell Script: /opt/admin/fastpg/bin/fastpg.exe -c -=NET (using fastpg.exe as a search option) 2. display = "Commented" (when its commented) and display = "Uncommented" (when its uncommented) Its urgent, please let me asap!!! Thanks in... (2 Replies)
Discussion started by: anthonyraj75
2 Replies

3. Shell Programming and Scripting

Help needed regarding first 3 items in the list

Hi, I've a list in the following format: Empdept filedetails buildingNo Area AAA 444 2 juy AAA 544 2 kui AAA 567 4 poi AAA 734 5 oiu AAA 444 ... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

4. Shell Programming and Scripting

List content of item in the combobox

I have a combo.cgi here. this is linux environment What i am going to do is this combobox will list down all the flatfile name in this /u/test/cgi-bin/List directory. after that, i wanted it to open the flatfile and display the content of the flatfile into another listbox or textarea in this page... (0 Replies)
Discussion started by: chezy
0 Replies

5. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

6. Shell Programming and Scripting

Removing lines from a file being used by another process using SHELL script

Hi All, Need a small help in writing a shell script which can delete a few lines from a file which is currently being used by another process. File gets appended using tee -a command due to which its size is getting increased. Contents like : 25/09/2012 05:18 Run ID:56579677-1 My... (3 Replies)
Discussion started by: nikhil8
3 Replies

7. UNIX for Dummies Questions & Answers

Easiest way to comment/uncomment a shell script?

cd path line1 line2 line3 line4 line5 Lets say thats the sample script...So say if i have to comment the above script, which would be the better way so that whenever i want, i cud comment or uncomment the same. Thanks (1 Reply)
Discussion started by: saggiboy10
1 Replies

8. Shell Programming and Scripting

Need help with a script to grep items in one file from another file

I have one master file "File1" with all such info in it. I need to grep each object under each list from another file "File2". Can anyone help me with a script for this. File 1 ------ List 1 Object 1 Object 2 List 2 Object 3 Object 1 List 3 Object 2 ... (5 Replies)
Discussion started by: Sam R
5 Replies

9. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

10. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies
paste(1)						      General Commands Manual							  paste(1)

NAME
paste - merge same lines of several files or subsequent lines of one file SYNOPSIS
file1 file2 ... list file1 file2 ... list] file1 file2 ... DESCRIPTION
In the first two forms, concatenates corresponding lines of the given input files file1, file2, etc. It treats each file as a column or columns in a table and pastes them together horizontally (parallel merging). In other words, it is the horizontal counterpart of cat(1) which concatenates vertically; i.e., one file after the other. In the option form above, replaces the function of an older command with the same name by combining subsequent lines of the input file (serial merging). In all cases, lines are glued together with the tab char- acter, or with characters from an optionally specified list. Output is to standard output, so can be used as the start of a pipe, or as a filter if is used instead of a file name. recognizes the following options and command-line arguments: Without this option, the new-line characters of all but the last file (or last line in case of the option) are replaced by a tab character. This option allows replac- ing the tab character by one or more alternate characters (see below). list One or more characters immediately following replace the default tab as the line concatenation character. The list is used circularly; i.e., when exhausted, it is reused. In parallel merging (that is, no option), the lines from the last file are always terminated with a new-line character, not from the list. The list can contain the special escape sequences: (new-line), (tab), (backslash), and (empty string, not a null character). Quoting may be necessary if charac- ters have special meaning to the shell. (For example, to get one backslash, use ). Merge subsequent lines rather than one from each input file. Use tab for concatenation, unless a list is specified with the option. Regardless of the list, the very last character of the file is forced to be a new-line. Can be used in place of any file name to read a line from the standard input (there is no prompting). EXTERNAL INFLUENCES
Environment Variables determines the locale for the interpretation of text as single- and/or multi-byte characters. determines the language in which messages are displayed. If or is not specified in the environment or is set to the empty string, the value of is used as a default for each unspecified or empty variable. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multi-byte character code sets are supported. RETURN VALUE
These commands return the following values upon completion: Completed successfully. An error occurred. EXAMPLES
List directory in one column: List directory in four columns Combine pairs of lines into lines Notes works similarly, but creates extra blanks, tabs and new-lines for a nice page layout. DIAGNOSTICS
Except for the option, no more than - 3 input files can be specified (see limits(5)). AUTHOR
was developed by OSF and HP. SEE ALSO
cut(1), grep(1), pr(1). STANDARDS CONFORMANCE
paste(1)
All times are GMT -4. The time now is 12:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy