Moving cat values to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving cat values to a variable
# 1  
Old 06-13-2014
Moving cat values to a variable

I have a file created as ABC!DEF@2014.txt

My if condition is based on 2014 so I need to move it to variable.

So while I can do this on console screen -
Code:
 ls ABC* -l > test.txt
cat test.txt | cut -f 2 -d "@" | cut -f 1 -d "."

to get the value - 2014
I am a bit at loss how to achieve this in a bash script.
# 2  
Old 06-13-2014
Code:
VAR=$(cat test.txt | cut -f 2 -d "@" | cut -f 1 -d ".")

to check:
Code:
 echo $VAR

# 3  
Old 06-13-2014
What happens if ABC* matches more than one file..
# 4  
Old 06-13-2014
A tighter search and allowing for multiple files matching ABC*@*.txt and avoiding a UUOC in ksh:-
Code:
for file in ABC*@*.txt
do
   tfile="${file%.txt}"
   tfile="${tfile#*@}"

   echo "I've found a match for $tfile in $file"
done


Does that help, or just confuse?

Robin

Last edited by rbatte1; 06-13-2014 at 08:28 AM.. Reason: Added link for UUOC
# 5  
Old 06-13-2014
Longhand using CygWin default bash terminal...
Code:
AMIGA:~> cd /tmp
AMIGA:/tmp> > 'ABC!DEF@2014.txt'
AMIGA:/tmp> var=`ls 'ABC!DEF@2014.txt'`
AMIGA:/tmp> echo "${var:$((${#var}-8)):4}"
2014
AMIGA:/tmp> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add space in front of cat values?

Hi, Hope you are all doing fine. The problem today i faced during my coding was i wanted to add a space equals to a tab character in front of all the lines which i am cat using tee command. Main file contents mainfile ... (4 Replies)
Discussion started by: mad man
4 Replies

2. Shell Programming and Scripting

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):... (3 Replies)
Discussion started by: paperbackwriter
3 Replies

3. UNIX for Beginners Questions & Answers

Carriage return with cat variable

Hi, I wish to know how to works the carriage return with cat.. As a picture often speaks better than words, my code below : #te=`cat text.txt` (I tried this, but the same..) te=$(cat text.txt) echo $te My file text.txt below: BLABLABLABLABLALBLA BLABLABLABLABLALBLA ... (9 Replies)
Discussion started by: Arnaudh78
9 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 Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

6. UNIX for Dummies Questions & Answers

format values in a text file using cat command

Hello... Is it possible that we can change the format of the values we entered in a text file using cat? Like for example, I will create a text file names.txt using cat and as I save the names and view the text file... the format will be like this ... (5 Replies)
Discussion started by: kpopfreakghecky
5 Replies

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

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

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

10. UNIX for Dummies Questions & Answers

How to Display a range of values in the output of cat

When I use this command I get an output of some numbers cat ac.20070511 | cut -d" " -f19 Is there any way for me to display only the numbers that are greater than 1000 but not all the numbers in the ouput. Can any one help me with this. :) (8 Replies)
Discussion started by: venu_nbk
8 Replies
Login or Register to Ask a Question