Quantcast
Channel: UNIX and Linux Forums
Viewing all 16232 articles
Browse latest View live

Rename files based on simple text file

$
0
0
Hello!

New here although not completely new to Unix.
I wonder how I could rename files based on the data found in a simple textfile.
It goes like this:

I have 4 files
Code:

1 ldfgkkfjslkdfjsldkfjsf.wav
2 nndsdflksdjf.wav
3 sdflksjdf jjsdflsdfl.wav
4 dkadsdddd.wav

Textfile.txt looks like this:
Code:

1 ^t Some ^t Data
2 ^t Other ^t Info & Stuff
3 ^t Yet ^t More Data
4 ^t To the ^t End

^t being tab, or some other apropriate separation character

I want files to look like this:
Code:

01 - Some -Data
02 - Other - Info & Stuff
03 - Yet - More Data
04 - To the - End

I realize there's a problem if the text document does not match the number of files but that does not need to be error handled. It does not need to be fool proof, I will do this one folder at a time, checking for errors manually. Each folder contains some 10-30 files. I want leading zeroes up to 9 if possible but it's not extremely important.

Background: These are mediafiles converted from tape archives. The titles are in a separate textfile for each converted tape. Each tapes goes into a separate folder.


Moderator's Comments:
Please use CODE tags as required by forum rules!

Vi editor Line too Long Error

$
0
0
My file is 13 mb but it has big long lines.

i tried in vain vi -R filename

I tried in vainbash: vim: command not found

Code:

SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v
Can you please suggest?

How to start msg chat in cmd?

$
0
0
Hello,
How to start msg chat in cmd i search in google i dont find messenger in services.

VIOS entstat versus seastat for SEA

$
0
0
Hello,
I found that the packet counts given by entstat -d SEA is not the same as the total of the packet counts given by seastat -d SEA for the same interval of time.
Do anyone have an explanation for the difference. We recently got a recommendation that the throughput on an SEA should not exceed 250,000 packets/second, and we were wondering which one of the statistics we could monitor between entstat and seastat. Below is an example of the difference in count:

Code:

# lsdev | grep Shared
 ent15          Available            Shared Ethernet Adapter
 ent18          Available            Shared Ethernet Adapter
 # chdev -l ent18 -a accounting=enabled
 ent18 changed

Reset of the stats
Code:

seastat -d ent18 -c
 entstat -r ent18
 sleep 60;
 seastat -d ent18 |grep Packets | awk '{ SUM += $2} END { print SUM " packets_Transmit_per_second" }'
 seastat -d ent18|grep Packets | awk '{ SUM += $4} END { print SUM " packets_Receive_per_second" }'
 entstat -d ent18
 RESULTS seastat
1016 packets_Transmit_per_second
1748 packets_Receive_per_second
 
 RESULTS entstat
 Transmit Statistics:                          Receive Statistics:
 --------------------                          -------------------
 Packets: 3329                                Packets: 4135
 Bytes: 1938358                                Bytes: 2348071
 Interrupts: 33                                Interrupts: 3229

[bash] - Replace blank and string in csv file

$
0
0
Hi all,
i have a .csv file with only two columns, like:
Code:

Login;Status
Luca;S
Marco;
Stefano;
Elettra;S
Laura;
...

I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like:
Code:

Login;Status
Luca;Disabled
Marco;Enabled
Stefano;Enabled
Elettra;Disabled
Laura;Enabled
...

I try with awk and sed, many errors results :mad: i was not familiar with bash... so i hope in your help.
Thank's all.

awk to lookup stored variable in file and print matching line

$
0
0
The bash bash below extracts the oldest folder from a directory and stores it in filename
That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that
(once the leading zero is removed) will match a line in link. That is the lint to print in output.
There will always be only one exact match. Thank you :).

Code:

# oldest folder used analysis and version log created
dir=/home/cmccabe/Desktop/NGS/test
{
  read -r -d $'\t' time && read -r -d '' filename
} < <(find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0' | sort -z -r )
printf "The oldest folder is $filename, created on $time and analysis done using v1.3 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/Desktop/NGS/test/log

Result of bash:
R_2017_01_13_14_46_04_user_S5-00580-25-Medexome

input
Code:

http://xxx.xx.xxx.xxx/output/Home/Auto_user_S5-00580-25-Medexome_135_080/plugin_out/FileExporter_out.194/R_2017_01_13_14_46_04_user_S5-00580-25-Medexome.tar.bz2
http://xxx.xx.xxx.xxx/output/Home/Auto_user_S5-00580-24-Medexome_136_078/plugin_out/FileExporter_out.191/R_2017_01_13_12_11_56_user_S5-00580-24-Medexome.tar.bz2

link
Code:

http://xxx.xx.xxx.xxx/output/report/latex/80.pdf
http://xxx.xx.xxx.xxx/output/report/latex/78.pdf

awk attempt with explanation
Code:

awk '{match(VAL=substr($0,RSTART,RLENGTH);match($0,/R*_[0-9]+\//);VAL1=substr($0,RSTART,RLENGTH);gsub(/.*_0|.*_|\/);print' $filename < inputlink > output
explanation
Code:

R_2017_01_13_14_46_04_user_S5-00580-25-Medexome extracted from $filename and matched to line 1 in input (section in bold)

that line has _080 in it (in italics)

the 80 (leading zero always removed), matches line1 in link so that is output

desired output this line matches the result from the bash so it is printed
Code:

http://xxx.xx.xxx.xxx/output/report/latex/80.pdf

Making sure only one instance of a script is running

$
0
0
so i have a script that takes a while to complete and its cpu intensive. this script is being used by several users. i want to make sure only 1 user can run this script at any given time.

i originally thought of running a while loop to egrep the process table of the PID ($$) of the process, but im not sure if that's going to be efficient. i want to make sure the script does not mistaking someone editing the script as a running process. so im excluding the common editing programs.

im searching for a solution that will be usable across all unix systems. here's what i have tried so far:

Code:

procname$(ps -ef | egrep "processname" | egrep -vc "grep| vi | ed | emacs")
if [ ${procname} -gt 0 ] ;then
echo "Script is currently in progress..aborting..."
exit 3
fi

Sftp command usage information

$
0
0
hello

how do we get detailed information of an sftp command?

for example when you type help on sftp, you get the available commands and their brief descriptions. What if you want to get detailed usage information, for example when you want to know what the command line options do..?

in unix, we do
Code:

$man command
..and it gives all details of the command. how do we accomplish this in sftp.

thanks

Fgrep literal string from a file

$
0
0
have a file1
Code:

aaa-bbb-ccc-abcd
aaa-bbb-ccc-bacd
aaa-bbb-ccc-aaad
aaa-bbb-ccc-a

have another file2
Code:

aaa-bbb-ccc-a file
using the fgrep command, trying to have only the literal string returned.

Code:

fgrep -f file2 file1
is returning
Code:

aaa-bbb-ccc-abcd
aaa-bbb-ccc-aaad
aaa-bbb-ccc-a

Only looking for
Code:

aaa-bbb-ccc-a
any ideas?

File group issues

$
0
0
Hi All,

I have a script which called by user id (mftp) . The set up is working on two server and not working one server.

Working Server :
Code:

-rw-rw-r-- 1  mftp  csp 0 Jan 17 00:14 117001.log
Non working server
Code:

-rw-rw-r-- 1  mftp  mftp 0 Jan 17 00:14 117001.log

from the above comparison I thought that mftp id was not csp group.So I added the mftp id to csp group. Even after that I am seeing the file is created like .

Code:

-rw-rw-r-- 1  mftp  mftp 0 Jan 17 00:14 117001.log

What should I make to create them as

Code:

-rw-rw-r-- 1  mftp  csp 0 Jan 17 00:14 117001.log
This is what the o/p of groups
Code:

groups mftp 
mftp  : mftp csp amp



Thanks in Advance

Percentages from line to line

$
0
0
i have a column of numbers and i need to find the percentage drops or increases from line to line.

suppose i have this:

Code:

45
14
14
2
8
8
8


the output im currently getting is this:

Code:

14
14,0%
2
8,75%
8
8,0%


which was produced by the following code:

Code:

awk '{prev=$1; print; getline; print $0","($1-prev)/$1*100"%"}'
the problem is when one of the numbers is a zero, awk bombs out. is there a way to modify this awk code to check to see if the numbers are zero, and if zero, it'll just assume a 0% change?

i basically have specific values for 24 hours. and i need to print the percentage drop or increase from hour to hour.

so lets assume the last 4 hours have the value:

20:00,120
21:00,50
22:00,39
23:00,90
00:00,70

the code im looking for will produce the following output:

Code:

20:00,120,-80%
21:00,50,-15%
22:00,39,-34%
23:00,90,+69%
00:00,70,-20%

note, the percentages above are wrong. i just put them there as an example.

but what im trying to do here is calculate the percentage drop or increases from one hour to the next.

i apologize if i inadvertently made this complicated.

0403-057 Syntax error at line 17 : `(' is not expected.

$
0
0
Hi,

While executing my code i am getting below Error:
Code:

./check_disk1[55]: 0403-057 Syntax error at line 55 : `(' is not expected.
My code is :

Code:

#!/bin/ksh

PROGNAME=`basename $0`
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

OS=$(uname)
AWK="/usr/bin/awk"
GREP="/usr/bin/grep"
DF="/usr/bin/df"

#############################
# Functions
#############################

print_help() {
echo ""
echo "$PROGNAME is a Nagios plugin used to check disk used space by"
echo "processing the output of \"df\" command. It runs on UNIX, Linux"
echo "and BSD platforms and reports the following performance data:"
echo "- total disk space (Bytes)"
echo "- currently used disk space (Bytes)"
echo "- currently used disk space (%)"
echo " "
echo "$PROGNAME Warning Critical Partition"
echo " "
}

WarnSpace=$1
CritSpace=$2
Partition=$3

CRITICAL_CONDITION=95
WARNING_CONDITION=90

# float number comparison
function fcomp() {
    $AWK -v n1=$1 -v n2=$2 'BEGIN{ if (n1<=n2) exit 0; exit 1}'
}


#formats bytes => KBytes, MBytes, GBytes, TBytes
function UNIX_btokmgt() {
        if [ $1 -lt 512 ]; then                  #KBytes
            echo "${1}KB"
        elif [ $1 -lt 262144 ]; then            #MBytes
            echo "$1" | $AWK '{printf "%.1fMB", $1/512}'
        elif [ $1 -lt 134217728 ]; then          #GBytes
            echo "$1" | $AWK '{printf "%.1fGB", ($1*512)/1073741824}'
        elif [ $1 -gt 134217728 ]; then        #TBytes
            echo "$1" | $AWK '{printf "%.1fTB",($1*512)/1099511627776}'
        fi
}

function btokmgt() {
        if [ $1 -lt 1024 ]; then                #Bytes
            echo "${1}B"
        elif [ $1 -lt 1048576 ]; then            #KBytes
            echo "$1" | $AWK '{printf "%.1fKB", $1/1024}'
        elif [ $1 -lt 1073741824 ]; then        #MBytes
            echo "$1" | $AWK '{printf "%.1fMB", $1/1048576}'
        elif [ $1 -lt 1099511627776 ]; then      #GBytes
            echo "$1" | $AWK '{printf "%.1fGB", $1/1073741824}'
        elif [ $1 -lt 1125899906842624 ]; then  #TBytes
            echo "$1" | $AWK '{printf "%.1fTB", $1/1099511627776}'
        fi
}

if [ $# -lt 1 ]; then
    print_help
    RESULT="UNKNOWN"
    RETURN_STATUS=$STATE_UNKNOWN
    exit $RETURN_STATUS
fi


if fcomp $WarnSpace 0
then
    WarnSpace=0
fi
if fcomp 100 $WarnSpace
then
    WarnSpace=100
fi

if fcomp $CritSpace 0
then
    CritSpace=0
fi
if fcomp 100 $CritSpace
then
    CritSpace=100
fi

if fcomp $CritSpace $WarnSpace
then
    WarnSpace=$CritSpace
fi


if [[ $OS == AIX ]]; then
    USEDTXT=`$DF -P $Partition 2>&1`
        #echo "USEDTXT value is $USEDTXT"
else
    USEDTXT=`$DF -P -B 1 $Partition 2>&1`
fi

#if [ $? != 0 ]
#then
#        echo "Error! Disk partition $Partition can't be checked. Does it exist?"
#        exit 3
#fi

if [[ $OS == AIX ]]; then
        CAPACITY=$(df "$Partition" | awk '!/Filesystem/ { print $4 }' | sed 's/%//')
#              echo "CAPACITY is $CAPACITY"
else
        CAPACITY=$(df -h "$Partition" | awk '!/Filesystem/ { print $4 }' | sed 's/%//')
fi


SpaceTxt=`echo "$USEDTXT" | $GREP "${Partition}\$"`
SpaceTotal=`echo "$SpaceTxt" | $AWK '{print $2}'`
SpaceUsed=`echo "$SpaceTxt" | $AWK '{print $3}'`
SpaceFree=`echo "$SpaceTxt" | $AWK '{print $4}'`
SpaceUsedProc=`echo "$SpaceTxt" | $AWK '{printf "%.1f", $3*100/$2}'`

SpaceFreeProc=`echo "$SpaceTxt" | $AWK '{printf "%.1f", $4*100/$2}'`

WarnSpaceAbs=`echo "$SpaceTotal $WarnSpace" | $AWK '{printf "%d", $1*$2/100}'`
CritSpaceAbs=`echo "$SpaceTotal $CritSpace" | $AWK '{printf "%d", $1*$2/100}'`
#echo "SpaceTxt value is $SpaceTxt"
#echo "SpaceTotal value is $SpaceTotal"
#echo "SpaceUsed value is $SpaceUsed"
#echo "SpaceFree value is $SpaceFree"
#echo "SpaceUsedProc value is $SpaceUsedProc"

if [[ $OS == AIX ]]; then
    SpaceTotal_F=`UNIX_btokmgt $SpaceTotal`
    SpaceUsed_F=`UNIX_btokmgt $SpaceUsed`
    SpaceFree_F=`UNIX_btokmgt $SpaceFree`
#      echo "SpaceTotal_F is $SpaceTotal_F"
#      echo "SpaceUsed_F is $SpaceUsed_F"
#      echo "SpaceFree_F is $SpaceFree_F"
else
    SpaceTotal_F=`btokmgt $SpaceTotal`
    SpaceUsed_F=`btokmgt $SpaceUsed`
    SpaceFree_F=`btokmgt $SpaceFree`
fi

PerfData="'used space'=${SpaceUsed}B;${WarnSpaceAbs};${CritSpaceAbs};0;${SpaceTotal} 'used space (pct.)'=${SpaceUsedProc}%;${WarnSpace};${CritSpace};0;100"
#echo "PerfData is $PerfData"
WarnSpace=$1
CritSpace=$2
CRITICAL_CONDITION=95
WARNING_CONDITION=90




if [[ $CritSpace < $CAPACITY ]]; then
    PS_STATUS="'$Partition' is at ${CAPACITY}% capacity, total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
    RESULT="CRITICAL"
    RETURN_STATUS=$STATE_CRITICAL
    FINAL_STATUS="$RESULT: ${PS_STATUS}"
    echo $FINAL_STATUS
    exit $RETURN_STATUS
elif [[ $WarnSpace < $CAPACITY ]]; then
    PS_STATUS="'$Partition' is at ${CAPACITY}% capacity, total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
    RESULT="WARNING"
    RETURN_STATUS=$STATE_WARNING
    FINAL_STATUS="$RESULT: ${PS_STATUS}"
    echo $FINAL_STATUS
    exit $RETURN_STATUS
else
    PS_STATUS="'$Partition' is at ${CAPACITY}% capacity, total ${SpaceTotal_F}, used ${SpaceUsed_F} (${SpaceUsedProc}%), free ${SpaceFree_F} (${SpaceFreeProc}%) | $PerfData"
    RESULT="OK"
    RETURN_STATUS=$STATE_OK
    FINAL_STATUS="$RESULT: ${PS_STATUS}"
    echo $FINAL_STATUS
    exit $RETURN_STATUS
fi

echo $FINAL_STATUS
exit $RETURN_STATUS

Please help me.
Thanks.

Help with detect with regex and move script

$
0
0
Hi all,

I am needing some help with a script that will search for a video file by known extensions and then do a pattern search (I'm guessing via regex) and then based on a match of one type of another move the file to an assigned directory.

I would like to do this with either a shell script such as bash or python however, I want to keep it as simple and compact as possible.

Examples

I have a folder that I have downloaded video files to, they are generally a mix of mp4, mkv, avi etc. and some will be movie files of various description and other will be tv episode that will either be named like mytvshow sxxexx (as in the sxx would be season number like s01 and the exx would be episode number like e02 for example) with a video file extension.) so the file may look like the following
TV show;
mytvshow s01e02.mp4
My Movie
my movieName.mkv

So the script needs to look at the extension or use another process to determine it is a video file and then based on the naming convension move the respective video file to a defined folder like

mytvfiles moved to /home/tv
mymovefiles moved to /home/movie

Would the scripting gurus out there help me by pointing me in the right direction with some examples of what i may use and suggestions on what may be best to suit this requirement i.e python or bash or something else maybe?

Thank you in advance for any assistance offered.

Cheers,
Darren.

Log all the commands input by user at real time in /var/log/messages

$
0
0
Below is my script to log all the command input by any user to /var/log/messages. But I cant achieve the desired output that i want. PLease see below.

Code:


function log2syslog
{
  declare COMMAND
  COMMAND=$(fc -ln -0)
  logger -p local1.notice -t bash -i -- "$USER:$COMMAND"
}
trap log2syslog DEBUG

Desired Output:
HTML Code:

Jan 13 17:09:05 SERVER1 bash[727]: user1: ls -l
Jan 13 17:09:05 SERVER1 bash[731]: user1:  hostname
Jan 13 17:09:05 SERVER1 bash[735]: user5: uname -a
Jan 13 17:09:05 SERVER1 bash[739]: user2: clear

Problem in taking input Shell script

$
0
0
I was writing a shell script, where i need to run a command through script and then take input from user and later terminate it. i am not sure how to take input in a proper format immediately after a command.

example: below command line is used to import some data in to database (LDAP)
Code:

ldapmodify -h "localhost" -p "14389" -D "cn=Directory Manager" -j "$dmpass"
after entering this command it expects the input from user in below format

Code:

*****************************************************
dn: uid=$b, ou=People,dc=p3chem,dc=net
changetype: modify
replace: BusinessCategory
BusinessCategory: Shipped 
******************************************************

above LDIF file changes the status of particular user in DataStore (LDAP).

now, my doubt is how to write a script in such a way that it uses the command :
Code:

ldapmodify -h "localhost" -p "14389" -D "cn=Directory Manager" -j "$dmpass"
to import LDIF file and then take the input in above format...

If i give the above 4 lines in the same format just after the Ldapmodify command it throws error

How can i split this.. :)?

$
0
0
hello, :)
How can i split this.. :)

10.25.10.2

two octet

a=2
b=5

Thank you...

Bash to store result in variable for other lines in script to use

$
0
0
I can not figure out how to capture the $filename variable store by the bash.

Code:

#!/bin/bash

# oldest folder stored as variable for analysis, version log created, and quality indicators matched to run
dir=/home/cmccabe/Desktop/NGS/test

find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -rz |
while read -r -d $'\t' time && read -r -d '' filename
do
    printf "The oldest folder is $filename, created on $time and analysis done using v1.4 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/medex.logs/folder.log
    awk -v FL="$filename" '
        FNR == 1 {filenum++}
        filenum==1 && index($0, FL) {
              match($0, "_0*([0-9]+)/")
              FNUM=substr($0,RSTART+1,RLENGTH-2)
              gsub(/^0+/,"", FNUM)
          }
          filenum==2 && $0 ~ FNUM".pdf$"' /home/cmccabe/s5_files/downloads/list /home/cmccabe/s5_files/pdf/pdf > /home/cmccabe/s5_files/pdf/output
    break
done

echo $filename

So after running the bash, $filename is 12345R12.

So, the result of the echo is 12345R12. Currently after the bash runs the $filename is not stored (maybe because of the done).

The result stored in $filename needs to be available for my script to execute. Currently, I manually copy it in but I thought that this might be a better way, but I can not seem to get the syntax correct. Thank you :).

How do I select certain columns with matching pattern and rest of the lines?

$
0
0
I want to select 2nd, 3rd columns if line has "key3" and print rest of the lines as is.

Code:

# This is my sample input
key1="val1" key2="val2" key3="val3" key4="val4"
some text some text
some text some text
key1="val1" key2="val2" key3="val3" key4="val4"
some text some text
some text some text

I have this so far which is not working. How do I add second condition?
Code:


awk '(/key3/ {print $2" "$3}) || ({print $0})'

awk join lines based on keyword

$
0
0
Hello ,

I will need your help once again.

I have the following file:

Code:

cat file02.txt
PATTERN XXX.YYY.ZZZ. 500
ROW01  aaa. 300 XS 14
ROW 45 29 AS XD.FD.
PATTERN 500 ZZYN002
ROW gdf gsste
ALT 267 fhhfe.ddgdg.
PATTERN ERE.MAY. 280
PATTERRNTH 5000 rt.rt.
ROW SO a 678
PATTERN dsjsdh.sdshb 400 80
PATTERN ssds.500. 60
ROW 3389 LAST ROW

I'm trying to join all the lines which start with pattern
Code:

PATTERN
. Also I need to remove the last . if occurs .

The desired results should be:

Code:

XXX.YYY.ZZZ 500 aaa 300 XS 14 45 29 AS XD.FD
500 ZZYN002 gdf gsste  267 fhhfe.ddgdg
ERE.MAY 280 5000 rt.rt SO a 678
dsjsdh.sdshb 400 80
ssds.500 60 3389 LAST ROW

I somehow managed to join the lines but cannot figure out how to get rid of the word PATTERN from output , remove the dot and delete the first word from the lines to be joined.

The command I came with is:
Code:

awk '/PATTERN/ && c{print c;c=""}{c=c $0" "}END{if(c) print c}' file02.txt
which produces (in red are the words / characters I don't need):
Code:

PATTERN XXX.YYY.ZZZ. 500  ROW01  aaa. 300 XS 14 ROW 45 29 AS XD.FD.
PATTERN 500 ZZYN002 ROW gdf gsste  ALT 267 fhhfe.ddgdg.
PATTERN ERE.MAY. 280 PATTERRNTH 5000 rt.rt. ROW SO a 678
PATTERN dsjsdh.sdshb 400 80
PATTERN ssds.500. 60 ROW 3389 LAST ROW

Thanks in advance for your help.

Need help with ls command

$
0
0
Hi,

I have the following files in my directory.

Code:

hello.txt1
hello.txt_bkp
hello.txt

I wish to list only these two files.

Code:

hello.txt1
hello.txt_bkp

ls -ltr *.txt* displays all the three instead of two.

How can I ?.... please suggest.
Viewing all 16232 articles
Browse latest View live




Latest Images