Two question: remove from the other variable or file to get another variable or file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two question: remove from the other variable or file to get another variable or file
# 1  
Old 11-22-2013
Two question: remove from the other variable or file to get another variable or file

Question 1:

Code:
A="a b c d e f g 1 2 3 4 5"
B="c 3"

get

Code:
C="a b d e f g 1 2 4 5"

Question 2:

Code:
cat file1
a
b
c
d
e
f
g
1
2
3
4
5

Code:
cat file2
c
3

get file3
Code:
a
b
d
e
f
g
1
2
4
5

# 2  
Old 11-22-2013
Solution1
Code:
for i in `echo "$A"`
do
flag=0
for j in `echo "$B"`
do
[[ "$j" == "$i" ]]  && flag=1
done
[[ $flag -eq 0 ]] && echo -n "$i "
done
echo ""

Solution2
Code:
grep -v -f file2 file1

# 3  
Old 11-22-2013
Try :

Solution 1:

Code:
#!bin/bash

A="a b c d e f g 1 2 3 4 5"
B="c 3"
C=$A

for i in $B; do

    if [ $(echo $A | grep "$i"| wc -l) -eq 1 ];then
        C=$(echo $C | sed 's/'$i'//g'| tr -s ' ' ' ')
    fi

done

printf "A = $A\nB = $B\nC = $C\n"

Code:
#!bin/bash

A="a b c d e f g 1 2 3 4 5"
B="c 3"
C=$(echo $A | sed 's/'$(echo $B | sed 's/[[:space:]]/\\\|/g')'//g' | tr -s ' ' ' ')

printf "A = $A\nB = $B\nC = $C\n"

Resulting

Code:
A = a b c d e f g 1 2 3 4 5
B = c 3
C = a b d e f g 1 2 4 5

Solution 2 :

Code:
$ awk 'FNR==NR{A[$1]++;next}!A[$1]' file2 file1

Code:
$ comm -23 <(sort file1) <(sort file2) | sort -n

Code:
$ join -v 1  <(sort file1) <(sort file2) | sort -n

Code:
$ diff -y  --suppress-common-lines  file1 file2 | cut -f1

Code:
$ sdiff --suppress-common-lines  file1 file2 | cut -f1

Resulting
Code:
a
b
d
e
f
g
1
2
4
5


Last edited by Akshay Hegde; 11-22-2013 at 08:59 AM..
# 4  
Old 11-23-2013
For recent shells (bash, ksh) and sed allowing for EREs, try
Code:
 sed -r "s/ +(${B// /|}) +/ /g" <<< $A

EDIT: Sorry, "here strings" (<<<) are only available in bash. use "here documents" (<< word ... word) in other cases.

Last edited by RudiC; 11-23-2013 at 11:26 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-24-2013
Q1:
Given these variables:
Code:
A="a b c d e f g 1 2 3 4 5"
B="c 3"

You could try:

Code:
C=
for i in $A; do
  for j in $B; do
    if [ "$i" = "$j" ]; then
      continue 2
    fi
  done
  C="$C$i "
done
printf "%s\n" "${C% }"


--
Quote:
Originally Posted by RudiC
[..]Sorry, "here strings" (<<<) are only available in bash. use "here documents" (<< word ... word) in other cases.
The here-string has been part of ksh93 since day one (and also zsh BTW)

Last edited by Scrutinizer; 11-24-2013 at 11:15 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-24-2013
Not being familiar with ksh, I checked this forum's man pages, and I did not find neither "here string" nor "<<<". That's why...
# 7  
Old 11-24-2013
Yes I presumed the man page for ksh93 on AT&T's website would be the original man page. I stand corrected. According to this change log, <<< was introduced in Release ksh93m+ in the year 2002.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Calling a variable of variable from a file

Hi All, I have file which have looks like below abc=${def} def=${efg} efg= "this is the actual value" based on "abc" value I have to call "efg" value , Am using below lines but it is not working #!/bin/bash source file.txt echo $abc Please wrap all code, files, input &... (5 Replies)
Discussion started by: Prashanth.K
5 Replies

3. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

5. Shell Programming and Scripting

Remove appending 0 from file variable

Dear All, i have filename RYK3201_032001002.pdf and i am using below command to get a file file_name1=$(echo $file_name | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6) and then file_name2=${NewFile_NAME}_$file_name1 now the value of file_name1 will be 032001 i want to file_name1... (5 Replies)
Discussion started by: yadavricky
5 Replies

6. Shell Programming and Scripting

Read value of a file, remove first 2 chars and put this value in a variable

Hi i have need of read a file value with cat command and remove first 2character for example cat /sys/class/rtc/day 0x12 Remove char 12 And put this value in a variable is possible with a script thanks for help (6 Replies)
Discussion started by: enaud
6 Replies

7. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

8. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

9. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

10. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies
Login or Register to Ask a Question