Why is my cut command not working ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why is my cut command not working ?
# 1  
Old 03-08-2018
Why is my cut command not working ?

OS : RHEL 6.8
Shell : bash


I want to remove all lines like below from the history output as it has password.


Code:
$ history | grep sqlplus
  239  sqlplus jn_usr/dxc825#@10.5.12.106/OCSGPD
  256  sqlplus osb_soa/KD1egM09@10.5.12.196/BSOAPRD
  279  sqlplus jn_usr/dxc825#@10.80.16.219/OCSGPD
  402  sqlplus jn_usr/dxc825#@10.80.16.219/MNXSPD
  868  sqlplus jn_usr/dxc825#@10.80.16.219/OCSGPD
  932  sqlplus osb_usr/vsNfCha92@10.5.12.106/OCSGPD
  934  sqlplus osb_usr/vsNfCha92@10.5.12.106/OCSGPD

To remove all the above lines ,I wanted to fetch the first field (239, 256, ....) from the above history command output and then run history -d in loop like below.

# untested
for h in $(history | grep sqlplus | cut -d' ' -f1); do history -d $h; done

But, the cut doesn't work as expected. I tried the following 2 options

The following returns blank
$ history | grep sqlplus | cut -d' ' -f1


The following returns lot of empty lines and few lines at the bottom as shown below

Code:
$ history | grep sqlplus | cut -d' ' -f2






1004
1021
1022
1023
1024
1026
1027
1028
1029
1030

# 2  
Old 03-08-2018
cut does not concatenate delimiters but increases the field count for every single one it encounters. Try
Code:
history | awk '/sqlplus/ {print $1}'

Be aware that the history offset changes with history lines deleted; you may need to sort them reversely.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-08-2018
Thank You Rudic.

One more question.
Any idea how I can get the below for loop to work in bash ?
I want to fetch each value from history | awk '/sqlplus/ {print $1}' command output and then run history -d on each value.
But, it doesn't seem to work well as shown below

Code:
$ for h in $(history | awk '/sqlplus/ {print $1}'); do history -d $h; done
-bash: history: 1019: history position out of range
-bash: history: 1031: history position out of range
-bash: history: 1032: history position out of range
-bash: history: 1033: history position out of range
-bash: history: 1034: history position out of range

# 4  
Old 03-08-2018
Code:
while [ -n "$(history | awk '/sqlplus/')" ] ; do history -d $(history | awk '/sqlplus/ {print $1; exit}') ; done


Last edited by rdrtx1; 03-08-2018 at 11:41 AM..
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 03-08-2018
I'm a bit puzzled. Although I warned that the history offset may change during the operations, the numbers should be "in range".
I don't really like the idea of working on "moving targets". rdrtx1's proposal nails one single history entry at a time, and chances are good this will work. Another untested / unproven option might be to update the history file (-a option?), dump the entire history to a file, eliminate all sqlplus entries, and write that updated file to the history file.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-08-2018
I have been playing around with this and it appears that multi-line commands go into the history as a single command until you log off. As an example if you typed a command like:

Code:
while read server ; do
    sqlplus jn_usr/dxc825#@$server/OCSGPD
done < hostlist

history will display:
Code:
  506  while read server ; do
    sqlplus jn_usr/dxc825#@$server/OCSGPD
done < hostlist
  507  cd ..

You might need some magic to identify and delete these history lines, or ensure you log back in before trying the delete.
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. UNIX for Dummies Questions & Answers

Cut command, no input delim, output delim not working

Hello, I'm using cygwin on my Windows 7 machine. From the man pages of cut: --output-delimiter=STRING use STRING as the output delimiter the default is to use the input delimiter I tried the following commands and got the error messages: $ cut -c1-10,20-30 -d... (10 Replies)
Discussion started by: kojac
10 Replies

3. Shell Programming and Scripting

Cut command not working in for loop

grep -Fxvf testdata.xls file_GTDA1.xls >file_GTDA.xls SLS_COUNT=`grep 'GTDA_Dly_Sls' file_GTDA.xls |wc -l` PMIX_COUNT=`grep 'GTDA_Dly_Pmix' file_GTDA.xls |wc -l` if ; then var1=`cat file_GTDA.xls|grep 'GTDA_Dly_Sls_'` var4="|" for i in $var1... (7 Replies)
Discussion started by: renuk
7 Replies

4. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

6. UNIX for Dummies Questions & Answers

cut not working the way i want it to

Hi Forum Im having problem with cut it even when i cut a field from an input file eg echo $x | cut -f1 -d':' it doesnt read the whole line if there is a space in it eg thisLineHasA SpaceInIt :wall: it only read up to the space.What i want is so the it cut the field as one line ... (8 Replies)
Discussion started by: ShinTec
8 Replies

7. Shell Programming and Scripting

why the set rr='echo string|cut not working

I am new to the c shell script, can you let me know why the set rr= is not working. C shell script #! /bin/csh Set tt= 12345_UMR_BH452_3_2.txt set rr='echo $tt | cut –d”_” -f1' syntax error (4 Replies)
Discussion started by: jdsignature88
4 Replies

8. Shell Programming and Scripting

cut the present working directory

how to traverse through each directory (1 Reply)
Discussion started by: Reddy482
1 Replies

9. UNIX for Dummies Questions & Answers

Cut not working in a loop

I have a function "MyPrint" that runs great on a file (BaseData.txt) that has one line of data. If i add rows to the text file it's reading the tFile variable becomes a list of every field 2 in the file. To correct this, i tried to call the function from a loop where i read one line at a time and... (4 Replies)
Discussion started by: KME
4 Replies

10. Solaris

/usr/bin/cut not working with largefiles on Solaris 10

I have a person running a perl script that is parsing > 2G log files and pipes to cut -d " " -f 1,6,7,8... The script itself is in a nfs mounted home directory. It runs fine when started from a solaris 8 box but fails after about 400 lines when started from the solaris 10 box. The solaris... (1 Reply)
Discussion started by: wottie
1 Replies
Login or Register to Ask a Question