LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-21-2019, 07:34 AM   #1
undefineduser
LQ Newbie
 
Registered: Nov 2019
Distribution: Arch
Posts: 9

Rep: Reputation: Disabled
Bash script - use current variable value as number to print a line


Hey,

I'm working on a copy script that will copy all files that diff says only appear in the original directory, to the backup directory.

Diff gives me this output, so I have to work a little with the string to get the right target path.
Quote:
Only in /usr/share/icons/Adwaita/16x16/apps: user-info1.png
Only in /usr/share/icons/Adwaita/48x48/apps: goa-panel-symbolic.symbolic1.png
Only in /usr/share/icons/Adwaita/48x48/apps: help-browser-symbolic.symbolic1.png
Only in /usr/share/icons/Adwaita/48x48/apps: web-browser1.png
My idea was to count the number of lines of the diff file and write it to a variable. Inside the loop I then write the correct path of the current line into a new variable (or array).
If I write awk 'NR==$i' my variable is empty, with 'NR=='$i'' i get an EOF.

Any tips of how I can improve/fix my script?

Code:
Quote:
> #!/usr/bin/env bash
> #set -x
>
> count=$(awk 'END{print NR}' diffoutput.txt)
>
> for((i="1";i<=$count;++i))
> do
>> filename=$(awk 'NR==$i' diffoutput.txt | awk '{print $NF}')
>> # ...
> done
> true

Last edited by undefineduser; 11-21-2019 at 07:48 AM.
 
Old 11-21-2019, 07:48 AM   #2
undefineduser
LQ Newbie
 
Registered: Nov 2019
Distribution: Arch
Posts: 9

Original Poster
Rep: Reputation: Disabled
Edit: Fixed it

I fixed it by adding -v i=$i!
Code:
 filename=$(awk -v i=$i 'NR==i' sample.txt | awk '{print $NF}')
 
Old 11-21-2019, 10:20 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
cp --no-clobber /path/to/original/* /path/to/new/
 
Old 11-21-2019, 06:26 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
wait, you only want the last field?

Code:
#!/bin/bash

echo awk
awk '{print $NF}' sample.txt
echo sed
sed -E 's/.+[[:blank:]]([[:graph:]]+)$/\1/' sample.txt
echo grep
grep -Eo [[:graph:]]+$ sample.txt 
echo bash
while read ;do printf "%s\n" "${REPLY##* }";done <sample.txt
echo or as array
while read -a ARRAY;do printf "%s\n" "${ARRAY[(-1)]}";done <sample.txt
note, those include both 16x16 and 48x48

the grep is not so useful if you need to factor that in

Code:
echo awk
awk '/16x16/{print $NF}' sample.txt
echo sed
sed -En 's/.+16x16.+[[:blank:]]([[:graph:]]+)$/\1/p' sample.txt
echo bash
while read
do
  [[ $REPLY =~ 16x16 ]] || continue
  printf "%s\n" "${REPLY##*[[:blank:]]}"
done <sample.txt
echo as array
while read -a ARRAY
do
  [[ ${ARRAY[(-2)]} =~ 16x16 ]] || continue
  printf "%s\n" "${ARRAY[(-1)]}"
done <sample.txt

#
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning a value to a variable also creates an empty file named as the value in bash StorageDon Linux - Newbie 3 11-01-2016 08:52 PM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
Find a field number for a value in text value - BASH severian23 Programming 3 01-10-2007 09:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration