Bash - Trying to cat a file that is a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - Trying to cat a file that is a variable
# 1  
Old 12-14-2016
Bash - Trying to cat a file that is a variable

Thank you in advance for looking at this, I've scoured the internet and can't find the answer I'm looking for!! - I am new at bash script so please bare with me!!

I have a script where I've identified individual files within a folder, the filename is then stored as a variable ($filename):
Code:
filename = ls -l | grep .csv | awk -v var="$filenum" '{if (NR==$filenum) print $0}' | awk -F " "  '{print $9}'

$filenum is used to tell the script how many files there are and will be used in a while loop to work on all the files in the folder.

I then want to able to cat $filename and display the files contents, rather than the variable. And all I keep getting is the variable - Please help!!!

Thanks again.

---------- Post updated at 08:57 AM ---------- Previous update was at 07:42 AM ----------

Thanks to those that have viewed this, I have now resolved it by using for f in ......
# 2  
Old 12-14-2016
Welcome paperbackwriter,

Please could you share your solution so others who may find this thread can see how to solve it for themselves.



Kind regards,
Robin

Last edited by rbatte1; 12-15-2016 at 09:51 AM.. Reason: Grammar!
# 3  
Old 12-14-2016
Hello paperbackwriter,

Welcome to forums, if you have to print only the file name with a given variable then you could make your life easy by trying following code once.
Code:
VAH="simple.test"
ls "$VAH"

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-14-2016 at 10:33 AM..
# 4  
Old 12-15-2016
Thanks for looking, I have copied this code from another site:

Code:
for f in *.pdf do
 echo "Removing password for pdf file - $f"
 #run commands on file here.......
 done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash: Insert in a variable a file

hi all i have a problem in the bash shell. i'd like insert in a variable a file for example : i have a file datafine.log in this file there is : 17/JUN/2019 i want to insert the value of datafine.log in a variable. Regards Frncesco edit by bakunin: please use CODE-tags for your data... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

2. 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

3. Shell Programming and Scripting

How to store remote variable from remote cat file ??

I am trying to cat on a file located on remote server and assign it to remote variable. I have both local and remote variables. Running below script from local. test.sh J_NAME=XXX2 J_IP=XXX ssh $J_IP "ps auxw |grep java | grep -v grep |grep $J_NAME | awk '{print ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

4. Shell Programming and Scripting

cat file with variable substitution

MyFile contains: ALTER TABLE $DBN.$TBN ADD $COL $TYP COMPRESS ($VAL); I need to cat the file and have it substitute all of the variables with their contents. cat MyFile does not work. The following works for the first line, but errors on the second line because of the paren: $ while read... (2 Replies)
Discussion started by: Phil27577
2 Replies

5. Shell Programming and Scripting

cat > variable + format

I wanted to store a value read from a file in a variable with a given format. I was doing the first step as follows : it0="$(cat ./myfile)" I thought the second step would work as: it0= ` printf "%6.3f\n" $it0 ` but it says "./curplot.sh: line 33: 0.035: command not found", although it... (2 Replies)
Discussion started by: josegr
2 Replies

6. Shell Programming and Scripting

cat vs head vs readline get variable from txt file

I have a file with a single filename in it, which I want to assign to a BASH variable, so I've been trying: c=$(head -1 somefile) echo $c which outputs correctly, but them when I do ... somecommand $c it says it can't find the file, is that because it's grabbing the whole line, and... (5 Replies)
Discussion started by: unclecameron
5 Replies

7. Shell Programming and Scripting

redirect cat to variable

hello just i saw a really strange for cat i have file (file1) contains line /home/rajiv/proj1/*.txt now applied a commonds DDPATH="$(cat file1)" echo $DDPATH it shows all the txt files in that folder like /home/rajiv/proj1/read1.txt /home/rajiv/proj1/read2.txt... (7 Replies)
Discussion started by: shailesh_arya
7 Replies

8. UNIX for Dummies Questions & Answers

Using a variable inside a file to cat another.

I have a question to do and it's somewhat confusing. It says, and I quote "Create a file called file_1 with three lines of text in it. Create a shell variable called "f_name", assign it the string "file_1". Use the cat command and the variable "f_name" to display the contents of the file... (3 Replies)
Discussion started by: MaestroRage
3 Replies

9. Shell Programming and Scripting

need help with my implementation of cat in bash

Hello, A few days ago I created my amateur version of the cat function in bash. Here is what I've done: #!/bin/bash #This is mycat. Similar to cat. #For detailed information use path/to/mycat.sh -h option arguments=$# if ] #in case of standard input then while ] do read input... (4 Replies)
Discussion started by: sanchopansa
4 Replies

10. Shell Programming and Scripting

variable= 'cat file|wc -l' String problems

Hi, does anybody knows about wc -l, how to transform it inot a just number? this script ALWAYS executes the command3!!, However, the value of BMU_RUNNING is 1 case $BMU_RUNNING in *0) command1 ;; *1) command 2;; *)command 3;; esac The... (3 Replies)
Discussion started by: Santiago
3 Replies
Login or Register to Ask a Question