Get the last field in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the last field in a file
# 1  
Old 07-10-2012
Get the last field in a file

Hi All,

I have a question, how do i get the last field inside a file. the file only consist of a single line.

Code:
$HOME/dir1/dir2/

i'm creating a script that will search for the directory and will prompt "directory not found" and will create that directory when not found.

Below is the if condition
Code:
if [ `find $HOME -name "tables" -type d -print -follow 2>/dev/null | wc -l` -eq 0 ]
then
   echo "Directory does not exists"
   echo "creating directory"
   mkdir tables
   echo "Done ..."
else
   echo "Directory exists"
fi

Please help,

Thanks

Last edited by Scrutinizer; 07-10-2012 at 04:12 AM.. Reason: code tags
# 2  
Old 07-10-2012
You can use awk and its NF Smilie
# 3  
Old 07-10-2012
basename command ?

Code:
 
$ echo $(basename /home/path/to)
to
$ echo $(basename /home/path/to/)
to
$ echo $(basename /home/path)
path

# 4  
Old 07-10-2012
@itkamaraj: In these recent days of "awk" and "sed", I almost forgot this Smilie
# 5  
Old 07-10-2012
THANKSSSSS!

tried to test which one is faster and boom basename for the kill

basename
Code:
$ time echo $(basename ${MOVE_DIR})
table

real    0m0.046s
user    0m0.000s
sys     0m0.015s

AWK
Code:
$ time echo $MOVE_DIR | awk -F '/' '{print $NF}'
table

real    0m0.052s
user    0m0.016s
sys     0m0.046s


Last edited by Scrutinizer; 07-10-2012 at 04:12 AM.. Reason: code tags
# 6  
Old 07-10-2012
check this also


Code:
 
$ MOVE_DIR=/home/path/to
$ time echo ${MOVE_DIR##*/}
to
real    0m0.000s
user    0m0.000s
sys     0m0.000s

$ time basename $MOVE_DIR
to
real    0m0.112s
user    0m0.003s
sys     0m0.013s

# 7  
Old 07-10-2012
This should be still faster:

Code:
echo ${MOVE_DIR##*/}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

5. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

6. Shell Programming and Scripting

Replacing field from file 1 with field from file 2

I've been searching these forums for the past 2 hours and i've found stuff that almost works but not quite. i have file 1: A, 1, Bla, 123 B, 2, Bla2, 123 C, 3, Bla3, 123 D, 4, Bla4, 123 file 2: 1, Aha 3, Ok Output: A, 1, Aha, 123 B, 2, Bla2, 123 C, 3, Ok, 123 D, 4, Bla4, 123 I... (7 Replies)
Discussion started by: jghi123
7 Replies

7. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

8. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

9. Shell Programming and Scripting

Need some help matching a field in one file with a field in another

One file (file1) is as such: Abc.txt 1.1 1.3 Abc_v2.txt 1.1 1.4 Tree.txt 1.3 1.4 Grass.txt 1.3 4.5 The other (file2): Horse.txt 1.1 ref23232 Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232 Plane.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232 1.4 Tree.txt 1.3 ref 3232-3232 1.4... (5 Replies)
Discussion started by: linuxkid
5 Replies

10. Shell Programming and Scripting

Sorting file by a field, and then by another field.

Hi all, Sorry the title is a mess, but did not find a better description at the time. So here is my problem: I have an input file: 8:Mass40s -- 0 48:Mass40s -- 0 67:Mass40s -- 0 86:Mass40s -- 0 105:Mass40s -- 0 9:Mass -- 1 49:Mass -- 86... (5 Replies)
Discussion started by: Alexis Duarte
5 Replies
Login or Register to Ask a Question